Can't get a simple code to work....why?

Started by
5 comments, last by fastcall22 14 years, 2 months ago
Hi, I started out with C++ a few weeks ago and had trouble compiling this code; my compiler(Dev-C++) gives me errors: #include<iostream> using namespace std; struct documentation { int age1; int age2; int age3; char name1[5]; char name2[5]; char name3[5]; }; int main() { string answer; documentation information; information.age1=38; information.age2=29; information.age3=41; information.name1="Paul"; information.name2="Mark"; information.name3="Adam"; cout<<"Please enter a name of the person you wish to find the age of: "; cin>>answer; cin.ignore(); switch (information) { case name1: cout<<age1; break; case name2: cout<<age2; break; case name3: cout<<age3; break; default: cout<<"Error: Name not found in the directory "; } cin.get(); } Can anyone please tell me what I'm doing wrong? Thanks
Advertisement
Your program is scary....!
It doesnt work because 'switch' does not know how to access data members of a class instantiation nor does it handle strings (only integers im afraid). rethink your program with this new found knowledge ;)

you may want to look into 2D arrays and for loops, although its not really clear here what you are trying to do.

  • Get rid of Dev-C++. Microsoft Visual Studio 2008 Express is much better.

  • The switch statement only works for integer types.

  • Use std::string instead of raw char-arrays. You cannot assign a char array to another char array, such as in information.name1="Paul";

  • Use an array to store your data instead of hardcoding each number of elements:
    struct Person {	string name;	int age;	Person( string n, int a ) {		name = n;		age = n;	}};vector<Person> people;people.push_back( Person( "James", 32 ) );people.push_back( Person( "Jannice", 33 ) );people.push_back( Person( "Josh", 34 ) );


  • Or, alternatively, you can use a map:
    map<string, int> people;people["James"] = 32;people["Jannice"] = 33;people["Josh"] = 34;string targetName;cin >> targetName;cout << people[targetName];


  • Use a for loop to check for names:
    string targetName;vector<Person> people;/* Fill people with Persons *//* request targetName */for ( unsigned i = 0; i < people.size(); ++i )	if ( people.name == targetName )		cout << people.age << endl;


Correction, the switch statement works with char types as well.
Quote:Original post by Dragonsoulj
Correction, the switch statement works with char types as well.


Correction, char is an integer type underneith it all.
Quote:Original post by Dragonsoulj
Correction, the switch statement works with char types as well.


But it doesn't work for strings:

The intent:
char fruit[255];cout << "Enter a fruit: ";cin.getline( fruit, 255 );if ( !strcmp( fruit, "apple" ) )	/* ... */ ;else if ( !strcmp( fruit, "banana" ) )	/* ... */ ;else if ( !strcmp( fruit, "grapefruit" ) )	/* ... */ ;/* or */string fruit;cout << "Enter a fruit: ";getline( cin, fruit );if ( fruit == "apple" )	/* ... */ ;else if ( fruit == "banana" )	/* ... */ ;else if ( fruit == "grapefruit" )	/* ... */ ;


Cannot be written with a switch statement:
char fruit[255];cout << "Enter a fruit: ";cin.getline( fruit, 255 );switch ( fruit ) {	case "apple": /* ... */ break;	case "banana": /* ... */ break;	case "grapefruit": /* ... */ break;}/* or */string fruit;cout << "Enter a fruit: ";getline( cin, fruit );switch ( fruit ) {	case "apple": /* ... */ break;	case "banana": /* ... */ break;	case "grapefruit": /* ... */ break;}

This topic is closed to new replies.

Advertisement