View Full Version : C++ Questions
Ricardus
03-21-2009, 06:46 AM
Hey guys,
I've been asked to do an small C++ program for sk00l (counts as this period's exam) that is to be delivered this Tuesday...
Present a 4 option menu and do the following operations:
1- Introduction of data (Name, Surname and Age)
2- Order de data (Ascending for example)
3- Search data (like a specific name)
4- Exit program (ya know... format c:) :eek:
Currently i'm having a problem with the 2nd part (Ordering)
To get the data i used a class (and it's working)
class pessoa {
public :
char nome[30] ;
char sobrenome[30] ;
int idade ;
} p[3] ;
Now to order the gathered data i'm trying the following coding:
void arranjar_dados()
{
char i , char j ;
char temp ;
for (i = 0 ; i < 3 ; i++)
{
for (j = (i + 1) ; j < 4 ; j++)
{
if (p[i].nome < p[j].nome)
{
temp = p[i].nome ; //error 1
p[i].nome = p[j].nome ; //error 2
p[j].nome = temp ; //error 3
}
}
}
}
Error 1 -> " '=' :cannot convert from 'char [30]' to 'char'
Error 2 -> " '=' :left operand must be |-value"
Error 3 -> " '=' :cannot convert from 'char' to 'char [30]'
This coding is supposed to look each array space and compare the [i] data with [j] data, if [j] is higher, it sends [i] to the temporary variable and then replaces it with [j], then [j] receives the value from Temp... theoreticly creating an ascending/descending kind of ordering... in number values (no clue on alphabetic stuff though).
by the errors i can assume that this sloppy coding would work much better in numerical values instead of char... saddly the only example i had to follow was an pseudo-code for numbers in a simple array, so translation errors are my fault.
I know that there's tons of examples scattered over the web but most of them are a bit over my head as a former military (PT Army Rangers)... (yeah i know, an 180 degree turn of life) :owned:
Soooo any help would be greatly appretiated
SheetiS
03-21-2009, 07:44 AM
The first thing I am seeing is you are as the the error says trying to drop a char [30] into a single char. This is like trying to fit 30 carts worth of groceries in a single cart. You can get past where you are by defining temp as char temp[30] to match what you have above.
There may be more issues which I will leave up to you to find, but that is the first thing that stuck out to me.
void arranjar_dados()
{
char i , char j ;
char temp[30] ; // This Here is what I changed.
for (i = 0 ; i < 3 ; i++)
{
for (j = (i + 1) ; j < 4 ; j++)
{
if (p[i].nome < p[j].nome)
{
temp = p[i].nome ; //error 1
p[i].nome = p[j].nome ; //error 2
p[j].nome = temp ; //error 3
}
}
}
}
legslat
03-21-2009, 10:23 AM
what sheetis said, the grocery thing was a prime example of what it was. Let me check something ill get back to you !
Ricardus
03-21-2009, 11:17 AM
Thanks for the fast replies guys :)
saddly (and after doing the char temp[30]) i'm still getting errors (atleast 2 different ones on the same troublesome 3 lines:
temp = p[i].nome ; //error 1
p[i].nome = p[j].nome ; //error 2
p[j].nome = temp ; //error 3
"Error C2106: '=' : left operand must be |-value" (for all 3 lines)
Man, this would be so much easier with normal numbers, but nooooo... it had to be classes :p
marcothy
03-21-2009, 12:54 PM
"Error C2106: '=' : left operand must be |-value"
This typically reflects an error around how you either trying to store variables, or ordering them.
I am not a C++ guy or I'd try to give you more info.
Demolition
03-21-2009, 01:48 PM
Post up a zip of your program. I'll look at it when I get up tonight.
Atten
03-21-2009, 01:51 PM
what are you ordering anyway Ricardus? Usually it's not a good idea to roll your own, if you're trying to order names alphabetically, might wanna use strcmp. Also you in that case might wanna swap the whole pessoa, not just the name - that sounds kinda wrong ;)
Also you can only have 3 persons based on your example - is that what you want?
Demolition
03-21-2009, 01:56 PM
what are you ordering anyway Ricardus? Usually it's not a good idea to roll your own, if you're trying to order names alphabetically, might wanna use strcmp. Also you in that case might wanna swap the whole pessoa, not just the name - that sounds kinda wrong ;)
Also you can only have 3 persons based on your example - is that what you want?
strcmp a character array? ;)
govtcheeze
03-21-2009, 01:56 PM
I havent done c/c++ in years, but AFAIR you have to use string functions for strings and operands for numbers. I think you need to use strcmp.
Demolition
03-21-2009, 01:59 PM
Oh, and to add more to my last post...
He is comparing in that function a single character from the name to a single character in a different name. Hes doing in essence the same thing the strcmp function would do, but given his use of a character array to store the names he can't use the strcmp function without rewriting his code a bit.
I havent done c/c++ in years, but AFAIR you have to use string functions for strings and operands for numbers. I think you need to use strcmp.
Not nec. You can overload the operands to handle strings, classes or w/e if you so choose.
Atten
03-21-2009, 03:21 PM
hmmm a character array is a string Demolition, not sure what you're after.
Ricardus
03-21-2009, 03:31 PM
Post up a zip of your program. I'll look at it when I get up tonight.
Rgr that Demo, but be warned...
...this stuff is so full of "Illegal Operations" that even Mafia Gangster would be put to shame!!
And remember... in case of a Police Raid, be fast to flush it down the toilet!! :evil5:
Update: Link to the *cough* Code (http://www.brokenaxes.com/Ricardus Project.zip) *code*
Demolition
03-21-2009, 10:55 PM
hmmm a character array is a string Demolition, not sure what you're after.
Hmm yea, Just re-read up on strcmp and you can use a character array. I was under the impression it only took string arguments
Mushok
03-21-2009, 10:56 PM
Man its been many many years since I have done C++. I was so quick to write a program too, if you can't figure it all out I could probly be not lazy and download the zip and finish it for ya.
Demolition
03-21-2009, 10:59 PM
Rgr that Demo, but be warned...
...this stuff is so full of "Illegal Operations" that even Mafia Gangster would be put to shame!!
And remember... in case of a Police Raid, be fast to flush it down the toilet!! :evil5:
Update: Link to the *cough* Code (http://www.brokenaxes.com/Ricardus Project.zip) *code*
If you could just zip your project folder as is that would be awesome. Then I can open it up and have everything how it is on your end. :)
Atten
03-22-2009, 08:28 AM
Hmm yea, Just re-read up on strcmp and you can use a character array. I was under the impression it only took string arguments
There is no "string" as such in C++, it's all based on basic data types - the closest thing to a "string" is probably the STL string class but that one has its own, separate functionality built on top of C++.
Ricardus
03-22-2009, 12:17 PM
If you could just zip your project folder as is that would be awesome. Then I can open it up and have everything how it is on your end. :)
Hurmm... i'm not sure if i follow you... the zip folder has every element (atleast when i try to DL it and open it)
...unless u mean "Hey n00b, u forgot the point WHICH one if the REAL project!!" :)
If the later, then it should be Projecto Bases 4 english.cpp
If the previous, i'll go do another zip
Thanks again for all your patience with a C++ n00b :)
Edit: New file link (http://www.brokenaxes.com/Projecto Bases 4 english.zip) posted and some more explanaition about the intended goal
Hail Drow!
Thanks for giving a hand to a fellow member :)
Ok, this is just a small project (for sk00l grades) that is intended to do the following operations:
- Present a 4 options menu
- The 1st option is to collect 3 elements of data from a number of ppl (preferably using Struct/Class)
(the ammount os ppl is of no importance, so i did a static 3 as time is short for more complicated stuff)
- The 2nd option should grab the previously introduced data, order it (the sort order used is not important) and print the results on the screen
- The 3rd option is to search for a specific element of data (like 2nd person's name) previously introduced and print it on the screen (showing 2nd person's data elements --> Name, Surname and Age)
- The 4th option exits the program (if i had more time, i would try to put some kind "Formating C: ...please wait" message using lots of "cout"s) ;)
Note: To check if the code was working more easily i started coding using separate projects with the intent of later join them all (after all parts were proved working) on the PROJECT BASES.
To speed-up checking code parts put also those individual cpp files on this zip, in case it will help.
Thanks again for any help :)
Ric
Demolition
03-22-2009, 03:30 PM
I assume your writing this with a IDE such as Visual Studio. This makes a folder with your debug sub folder ect ect. This also has a solution file there that when you click on it, it will load up your solution without me having to create the project and add in all the files myself. You'll save me some time if you do that. And right now, time is a huge thing for me.
Here is an example of what I mean when I said the whole project folder:
example (http://www.filefactory.com/file/af68507/n/Sample_zip)
That projects written in assembly, so its not going to do you any good in learning c++ but it will show you what I am looking for.
There is no "string" as such in C++, it's all based on basic data types - the closest thing to a "string" is probably the STL string class but that one has its own, separate functionality built on top of C++.
Thanks, I'm aware there is no "string" data type defined in C++ and that its a STL class provided. I was under the impression originally that strcmp only took strings as its arguments. As I already pointed out, I looked back at the info and found that's not the case. Lets get back to helping Ricardus now :)
Ricardus
03-22-2009, 03:51 PM
I assume your writing this with a IDE such as Visual Studio. This makes a folder with your debug sub folder ect ect. This also has a solution file there that when you click on it, it will load up your solution without me having to create the project and add in all the files myself. You'll save me some time if you do that. And right now, time is a huge thing for me.
Yep, i'm using Visual Studio 2005 (school edition since it's a copy of the original which we use in class), and sorry for not realizing what u meant before... being a n00b at this sux :p
Ok here goes the complete folder (http://www.brokenaxes.com/Projecto Bases 4.zip) zipped
Demolition
03-23-2009, 06:40 AM
Ok, first thing to do is in your arranjar_dados()
you decalred char i,j;
These should be integers for a loop to access correctly.
Second thing, and this is what is causing your errors.
Character arrays don't have a defined assignment operation for =
thats why those 3 errors there.
My suggestion is to not use character arrays to store your names, instead use a string, Those can be assigned with = and make your swap like you want.
Your not using English(I assume because its not your native tongue and your taking a class at your local college) so I can follow the definitions to an extent, but I almost want a translator :P
Ricardus
03-23-2009, 10:16 AM
Ok, first thing to do is in your arranjar_dados()
you decalred char i,j;
These should be integers for a loop to access correctly.
Second thing, and this is what is causing your errors.
Character arrays don't have a defined assignment operation for =
thats why those 3 errors there.
My suggestion is to not use character arrays to store your names, instead use a string, Those can be assigned with = and make your swap like you want.
Your not using English(I assume because its not your native tongue and your taking a class at your local college) so I can follow the definitions to an extent, but I almost want a translator :P
Hehe, sorry about the language problem, but u are correct, since i'm doing it at local collage and i started doing the program without planning in asking help... but sometimes well intended plans do hit the fan :o
Ricardus
03-23-2009, 12:23 PM
Ok, let the rush week start!!! :drow_bangHead:
Monday: Networks Exam
Tuesday: C++ Program
Wednesday: Math Exam
Thursday: Website Exam
Friday: Getting Drunk as a Skunk Exam :puke:
Saturday: End of Grading Period and Start of Vacations
:pottytrain5:
Ricardus
03-23-2009, 06:10 PM
Ok, here's an update on the code:
#include <iostream>
using namespace std ;
class pessoa
{
public :
char nome[30] ;
char sobrenome[30] ;
int idade ;
} p[3] ;
void obter_dados() ;
void ordena_dados() ;
int main()
{
int op ;
do
{
cout << "\nEscolha de 1 a 4:" << endl ;
cout << "" << endl ;
cout << "\n(1) - Criar Lista de Registos" << endl ;
cout << "\n(2) - Listar Registos Ordenados" << endl ;
cout << "\n(3) - Pesquisar Registos por Valor" << endl ;
cout << "\n(4) - Sair do Programa" << endl ;
cout << "" << endl ;
cin >> op ;
switch(op)
{
case 1:
cout << "\tEscolheu 1" << endl ;
obter_dados() ;
break ;
case 2:
cout << "\tEscolheu 2" << endl ;
ordena_dados() ;
break ;
case 3:
cout << "\tEscolheu 3" << endl ;
break ;
case 4:
cout << "\tEscolheu 4" << endl ;
break ;
default: // Se a variavel do switch não for nenhuma das anteriores
cout << "Apenas 1, 2, 3 ou 4!" << endl ;
break ;
}
cin.get() ;// Pede uma tecla para continuar
}
while(op!=4);
}
void obter_dados()
{
cout << "Introduza o Nome, Sobrenome e Idade de 3 pessoas" << endl ;
cout << "" << endl ;
cout << "Escreva o Nome da 1a pessoa: " ;
cin >> p[0].nome ;
cout << "Escreva o Sobrenome: " ;
cin >> p[0].sobrenome ;
cout << "Qual a Idade? " ;
cin >> p[0].idade ;
cout << "" << endl ;
cout << "Escreva o nome da 2a pessoa: " ;
cin >> p[1].nome ;
cout << "Escreva o Sobrenome: " ;
cin >> p[1].sobrenome ;
cout << "Qual a Idade? " ;
cin >> p[1].idade ;
cout << "" << endl ;
cout << "Escreva o nome da 3a pessoa: " ;
cin >> p[2].nome ;
cout << "Escreva o Sobrenome: " ;
cin >> p[2].sobrenome ;
cout << "Qual a Idade? " ;
cin >> p[2].idade ;
}
void ordena_dados()
{
int i , j ;
int temp_idade ;
char temp_nome[30] ;
char temp_sobrenome[30] ;
for (i = 0 ; i < 3 ; i++)
{
for (j = (i + 1) ; j < 4 ; j++)
{
if (p[i].nome < p[j].nome)
{
strcpy(temp_nome, p[i].nome) ;
strcpy(temp_sobrenome, p[i].sobrenome) ;
temp_idade = p[i].idade ;
strcpy(p[i].nome, p[j].nome) ;
strcpy(p[i].sobrenome, p[j].sobrenome) ;
p[i].idade = p[j].idade ;
strcpy(p[j].nome, temp_nome) ;
strcpy(p[j].sobrenome, temp_sobrenome) ;
p[j].idade = temp_idade ;
}
}
}
for (int i = 0 ; i < 3 ; i++ )
{
cout << "" << endl ;
cout << p[i].nome << endl ;
cout << p[i].sobrenome << endl ;
cout << p[i].idade << endl ;
}
return ;
}
- The Menu is now properly working without exit (unless we press the option 4 to leave as intended.
- It is collecting data.
- Although it is ordering the 2nd and 3rd person's data, it is eating the 1st person Name and Surname both and it's displaying a 0 on his age.
- The search code is not done yet but i'm thinking on a strcmp on each p[i].name (or along those lines) and then returning the data if a positive match.
- Dynamic Memory Allocation plans are discarded since i'm almost out of time (project delivery is tomorrow at 7pm GMT).
Thanks for the help guys :)
Edit: And here goes the link for an website (http://maias.brokenaxes.com/BTcover.htm) (for exam too on Thursday) that i did also yesterday (lack of sleeping made me forget to name the "Home" buttons at the bottom of the pages)
govtcheeze
03-23-2009, 06:23 PM
Standard disclaimer applies...have not written c++ code in years, but this is how I would do the last part.
void ordena_dados()
{
int i , j ;
int temp_idade ;
char temp_nome[30] ;
char temp_sobrenome[30] ;
for (i = 0 ; i < 2 ; i++) {
if (strcmp(p[i].nome, p[i+1].nome) > 0) {
strcpy(temp_nome, p[i].nome) ;
strcpy(temp_sobrenome, p[i].sobrenome) ;
temp_idade = p[i].idade ;
strcpy(p[i].nome, p[i+1].nome) ;
strcpy(p[i].sobrenome, p[i+1].sobrenome) ;
p[i].idade = p[i+1].idade ;
strcpy(p[i+1].nome, temp_nome) ;
strcpy(p[i+1].sobrenome, temp_sobrenome) ;
p[i+1].idade = temp_idade ;
}
}
}
for (int i = 0 ; i < 3 ; i++ ) {
cout << "" << endl ;
cout << p[i].nome << endl ;
cout << p[i].sobrenome << endl ;
cout << p[i].idade << endl ;
}
}
php uses count as an alias to sizeof, but to make your code more portable you can also do:
for($i = 0; $i < sizeof($p) - 1; $i++) or something similar (whatever the c++ variation is) so that you do not have to change your code if you add or remove elements to p. You want sizeof - 1 because you want to interate 1 less time than elements since you compare the current element to the next.
You may also need to change the
char temp_nome[30]; to
string temp_nome; and
char temp_sobrenome[30] ; to
string temp_sobrenome;
govtcheeze
03-23-2009, 06:32 PM
For search:
if(!strcmp($str1, $str2)) {
cout << whatever;
}It seems wierd, but strcmp returns 0 if the strings match so ! is correct. When I was learning how to use it, I read it to myself as "if not string compare different"...maybe that helps?
Ricardus
03-23-2009, 06:39 PM
Nice one bro... u kept it simple and easy :)
...cuz after the last 2 days sleeping about 3h per night/day sure is making me think as if i was swimming in porrige :p
Demolition
03-24-2009, 06:24 AM
Standard disclaimer applies...have not written c++ code in years, but this is how I would do the last part.
void ordena_dados()
{
int i , j ;
int temp_idade ;
char temp_nome[30] ;
char temp_sobrenome[30] ;
for (i = 0 ; i < 2 ; i++) {
if (strcmp(p[i].nome, p[i+1].nome) > 0) {
strcpy(temp_nome, p[i].nome) ;
strcpy(temp_sobrenome, p[i].sobrenome) ;
temp_idade = p[i].idade ;
strcpy(p[i].nome, p[i+1].nome) ;
strcpy(p[i].sobrenome, p[i+1].sobrenome) ;
p[i].idade = p[i+1].idade ;
strcpy(p[i+1].nome, temp_nome) ;
strcpy(p[i+1].sobrenome, temp_sobrenome) ;
p[i+1].idade = temp_idade ;
}
}
}
for (int i = 0 ; i < 3 ; i++ ) {
cout << "" << endl ;
cout << p[i].nome << endl ;
cout << p[i].sobrenome << endl ;
cout << p[i].idade << endl ;
}
}
php uses count as an alias to sizeof, but to make your code more portable you can also do:
for($i = 0; $i < sizeof($p) - 1; $i++) or something similar (whatever the c++ variation is) so that you do not have to change your code if you add or remove elements to p. You want sizeof - 1 because you want to interate 1 less time than elements since you compare the current element to the next.
You may also need to change the
char temp_nome[30]; to
string temp_nome; and
char temp_sobrenome[30] ; to
string temp_sobrenome;
I was trying to point him in the right direction without giving him the answer. If you solve it for him he won't learn :P
Ricardus
03-24-2009, 10:15 AM
I was trying to point him in the right direction without giving him the answer. If you solve it for him he won't learn :P
There is also Reverse-Engineering for learning purposes :p
The "why the heck this is working when it shouldn't?!?!?!?" :drow_bangHead:
(Thanks to it, and Govt, i learned why my coding was being shown in a lame manner, unlike Govt i was using the Wrap[QUOTE] forum thinggie instead of the Wrap
forum thinggie... reason is that i never bothered/remembered to check what all forum thinggies does... but i get curious when i see something done in a way i can't do) :coffee:
[CODE] {
yey ---> yey
}
Also i ran out of time... now i need to focus on Math (dang teacher ia making us study ALL the stuff since January instead of more specific stuff).
Time is yer enemy i tell yas!! :owned:
Constantine
03-24-2009, 06:34 PM
Ok, let the rush week start!!! :drow_bangHead:
Monday: Networks Exam
Tuesday: C++ Program
Wednesday: Math Exam
Thursday: Website Exam
Friday: Getting Drunk as a Skunk Exam :puke:
Saturday: End of Grading Period and Start of Vacations
Sunday: Re-up your Eve Online account!
Ricardus
03-24-2009, 08:49 PM
Sunday: Re-up your Eve Online account!
Heh, sorry Const but while i'm fully confident that EVE makers did pass their C++ exams (or they wouldn't be able to create such an in-depth and detailed game world), i'm not so sure on their "Customer Handling" exam results. :p
And after the FailCom's customer handling Fiasco, i'm running short on faith in gaming companies :p
(And remember that old saying "Once shafted, shame on you, Twice shafted, shame on me... and Thrice shafted by CCP means u are a retard!"... and also remember that with the "PosGate" scandal, it's my 2nd time shafted)
But since this is a C++ coding thread...
char player[10] = "Ricardus" ;
int CCP = 3 ;
float fun ;
fun = player + CCP ;
cout << "The amount of fun is: " << fun ;
*ERROR*
:puke:
Demolition
03-25-2009, 06:43 AM
There is also Reverse-Engineering for learning purposes :p
The "why the heck this is working when it shouldn't?!?!?!?" :drow_bangHead:
(Thanks to it, and Govt, i learned why my coding was being shown in a lame manner, unlike Govt i was using the Wrap[QUOTE] forum thinggie instead of the Wrap
forum thinggie... reason is that i never bothered/remembered to check what all forum thinggies does... but i get curious when i see something done in a way i can't do) :coffee:
[CODE] {
yey ---> yey
}
Also i ran out of time... now i need to focus on Math (dang teacher ia making us study ALL the stuff since January instead of more specific stuff).
Time is yer enemy i tell yas!! :owned:
You made my case for me. You saw something you wanted to do... you researched it. Now you know how to do it and how to learn more stuff you don't know.
Thanks :P
Constantine
03-25-2009, 10:33 AM
Ric, I was online last night moving stuff in both my accounts to our new base. Then I looked at my assets in kerepa and found a giant ball of trit (my standards). The agreement was that you were going to refine it and haul it *cracks whip*. Anyway, maybe in a few months you'll get lonely and come back :)
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.