![]() |
| | #1 |
| Registered User Join Date: Oct 2009
Posts: 16
| make a char display a word or letter Code: #include <iostream>
using namespace std;
struct band {
int age;
char instrument;
};
int main ()
{
int a;
int b;
cout<< "this program lets you learn about the band members age and instruments.\n";
cout<< "\n";
cout<< "for instruments:\n";
cout<< "1 = Singer\n";
cout<< "2 = Guitar\n";
cout<< "3 = Drums\n";
cout<< "\n";
cout<< "press enter to begin\n";
cin.get ();
cout<< "pick who you want to know about.\n";
cout<< "1 - Charlie\n";
cout<< "2 - Jake\n";
cout<< "3 - Matt\n";
cout<< "4 - Otis\n";
cin>> a;
switch (a){
case 1:
cout<< "you picked Charlie!\n";
cout<< "1 - age\n";
cout<< "2 - instrument\n";
cin>> b;
band charlie;
band *c;
charlie.age = 13;
charlie.instrument = 1;
c = &charlie;
if (b == 1)
cout<< c->age;
if (b == 2)
cout<< c->instrument;
break;
case 2:
cout<< "you picked Jake!\n";
cout<< "1 - age\n";
cout<< "2 - instrument\n";
cin>> b;
band jake;
band *j;
jake.age = 13;
jake.instrument = 2;
j = &jake;
if (b == 1)
cout<< j->age;
if (b == 2)
cout<< j->instrument;
break;
case 3:
cout<< "you picked Matt!\n";
cout<< "1 - age\n";
cout<< "2 - instrument\n";
cin>> b;
band matt;
band *m;
matt.age = 14;
matt.instrument = 2;
m = &matt;
if (b == 1)
cout<< m->age;
if (b == 2)
cout<< m->instrument;
break;
case 4:
cout<< "you picked Otis!\n";
cout<< "1 - age\n";
cout<< "2 - instrument\n";
cin>> b;
band otis;
band *o;
otis.age = 13;
otis.instrument = 3;
o = &otis;
if (b == 1)
cout<< o->age;
if (b == 2)
cout<< o->instrument;
break;
}
cin.get ();
}
|
| dyelax is offline | |
| | #2 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| A character is just that: a character. If you want a bunch of characters, the data type you seek is "string". |
| tabstop is offline | |
| | #3 |
| Registered User Join Date: Oct 2009
Posts: 16
| ya but i couldnt even get it to do a character. and how do you do a string? |
| dyelax is offline | |
| | #4 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Code: #include <string>
#include <iostream>
int main() {
std::string bob;
bob = "Hey look it's a bunch of characters!";
std::cout << bob << std::endl;
return 0;
}
|
| tabstop is offline | |
| | #5 |
| beginner for now Join Date: Oct 2009
Posts: 27
| well I know how to do for 1 caracter (char) but not for string Code: #include <iostream>
#include <string>
using namespace std;
int main ()
{
string select;
cin >> select;
switch ( select )
{
case 'start':
{
cout << "start" << endl;
break;
}
case '2':
{
cout << "load" << endl;
break;
}
default:
{cout << "wrong try again" << endl;}
}
system ("pause");
return 0;
}
and I gain error Code: Compiler: Default compiler Building Makefile: "C:\Programing\my own programs\some Projects\my Game\Makefile.win" Executing make... make.exe -f "C:\Programing\my own programs\some Projects\my Game\Makefile.win" all g++.exe -D__DEBUG__ -c "Game menu.cpp" -o "Game menu.o" -I"C:/Programing/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Programing/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Programing/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Programing/Dev-Cpp/include/c++/3.4.2" -I"C:/Programing/Dev-Cpp/include" -pg -g3 Game menu.cpp: In function `int main()': Game menu.cpp:18: error: switch quantity not an integer Game menu.cpp:20:17: warning: character constant too long for its type make.exe: *** ["Game menu.o"] Error 1 Execution terminated |
| military genius is offline | |
| | #6 |
| Registered User Join Date: Oct 2009
Posts: 16
| So how would I use that to make my program say the instruments names instead of a number? And how would you use char then? Sorry I'm asking so many questions haha. |
| dyelax is offline | |
| | #7 |
| beginner for now Join Date: Oct 2009
Posts: 27
| Code: #include <iostream>
#include <string>
using namespace std;
int main ()
{
char select;
cin >> select;
switch ( select )
{
case 'a':
{
cout << "start" << endl;
break;
}
case 'b':
{
cout << "load" << endl;
break;
}
default:
{cout << "wrong try again" << endl;}
}
system ("pause");
return 0;
}
try this for char 1 character must be between ' ---> 'a' Last edited by military genius; 10-12-2009 at 05:27 PM. Reason: something from my own program deleted |
| military genius is offline | |
| | #8 |
| Registered User Join Date: Oct 2009
Posts: 3
| Switch You can't switch on a string. |
| ipndrmath is offline | |
| | #9 |
| beginner for now Join Date: Oct 2009
Posts: 27
| truthfully to say this one was a hard one but I did it Code:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
int select;
string word;
string start;
string load;
start = "start";
load = "load";
getline (cin,word);
if (word == start)
{
select = 1;
}
else if (word == load)
{
select = 2;
}
switch ( select )
{
case 1:
{
cout << "start" << endl;
break;
}
case 2:
{
cout << "load" << endl;
break;
}
default:
{cout << "wrong try again" << endl;}
}
system ("pause");
return 0;
}
if anyone else says differently show me the code and test it before you show it it's easy only to replace int or char it's harder to try out because you can't only replace only if you do something more on that string you replace with |
| military genius is offline | |
| | #10 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| I've got to ask: what do you think you did? And how does it relate to the question? Quote:
| |
| tabstop is offline | |
| | #11 | |
| Registered User Join Date: Apr 2008 Location: Australia
Posts: 26
| Quote:
look at your char variables. maybe they should be; charlie.instrument = '1'; //and not 1; or instead, for string try; Code: struct band
{
int age;
char *instrument; //string will cause compile error with current code. but can use //this to see the effect
};
//in main where this is declared;
charlie.instrument = "voice"; //note the quotations
| |
| Tropod is offline | |
| | #12 |
| beginner for now Join Date: Oct 2009
Posts: 27
| why don't you think I have already try it out ??????????? ----->>>>> I sad try it your self first and than post whole code please here is your code you were talking about with error Code: #include <iostream>
#include <string>
using namespace std;
int main ()
{
string select;
cin >> select;
switch ( select )
{
case "start":
{
cout << "start" << endl;
break;
}
case "load":
{
cout << "load" << endl;
break;
}
default:
{cout << "wrong try again" << endl;}
}
system ("pause");
return 0;
}
Code: Compiler: Default compiler Executing g++.exe... g++.exe "C:\Programing\my own programs\showing some stuff just to show 1.cpp" -o "C:\Programing\my own programs\showing some stuff just to show 1.exe" -pg -g3 -I"C:\Programing\Dev-Cpp\lib\gcc\mingw32\3.4.2\include" -I"C:\Programing\Dev-Cpp\include\c++\3.4.2\backward" -I"C:\Programing\Dev-Cpp\include\c++\3.4.2\mingw32" -I"C:\Programing\Dev-Cpp\include\c++\3.4.2" -I"C:\Programing\Dev-Cpp\include" -L"C:\Programing\Dev-Cpp\lib" -lgmon -pg -g3 C:\Programing\my own programs\showing some stuff just to show 1.cpp: In function `int main()': C:\Programing\my own programs\showing some stuff just to show 1.cpp:11: error: switch quantity not an integer Execution terminated I have already show this in upper post but it seems I must again now before you post please try out your self |
| military genius is offline | |
| | #13 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
![]() |
| Tags |
| c++, char, letter, word |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Seg Fault in Compare Function | tytelizgal | C Programming | 1 | 10-25-2008 03:06 PM |
| Converting a circulating mouse pointer to use a Potentionmeter | phoenix23 | C Programming | 16 | 10-29-2006 05:04 AM |
| How do i un-SHA1 hash something.. | willc0de4food | C Programming | 4 | 09-14-2005 05:59 AM |
| Program Crashing | Pressure | C Programming | 3 | 04-18-2005 10:28 PM |
| Strings are V important... | NANO | C++ Programming | 15 | 04-14-2002 11:57 AM |