C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-12-2009, 03:54 PM   #1
Registered User
 
Join Date: Oct 2009
Posts: 16
make a char display a word or letter

so i made this simple program to learn about structures and pointers and i declared 1 variable a char but when i tried to make it equal a letter or word i got an error so i had to put it in as a number. id really like it to be able to display a word. how would i do this. thanks. heres my program:

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   Reply With Quote
Old 10-12-2009, 03:59 PM   #2
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 10-12-2009, 04:16 PM   #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   Reply With Quote
Old 10-12-2009, 04:21 PM   #4
and the Hat of Guessing
 
tabstop's Avatar
 
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   Reply With Quote
Old 10-12-2009, 05:19 PM   #5
beginner for now
 
Join Date: Oct 2009
Posts: 27
Unhappy

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   Reply With Quote
Old 10-12-2009, 05:22 PM   #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   Reply With Quote
Old 10-12-2009, 05:26 PM   #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   Reply With Quote
Old 10-12-2009, 09:11 PM   #8
Registered User
 
Join Date: Oct 2009
Posts: 3
Switch

You can't switch on a string.
ipndrmath is offline   Reply With Quote
Old 10-12-2009, 09:57 PM   #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   Reply With Quote
Old 10-12-2009, 10:03 PM   #10
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
Quote:
Originally Posted by military genius View Post
truthfully to say this one was a hard one
I've got to ask: what do you think you did? And how does it relate to the question?
Quote:
but I did it
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
I don't think any of this makes sense. "Easy only to replace int or char"? Replace it with what? And why? There's no replacing going on in your code. "It's harder to try out"? What?
tabstop is offline   Reply With Quote
Old 10-13-2009, 05:26 AM   #11
Registered User
 
Join Date: Apr 2008
Location: Australia
Posts: 26
Quote:
Originally Posted by dyelax View Post
ya but i couldnt even get it to do a character. and how do you do a string?


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   Reply With Quote
Old 10-13-2009, 11:19 AM   #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   Reply With Quote
Old 10-13-2009, 11:55 AM   #13
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,365
Quote:
Originally Posted by military genius
I have already show this in upper post
but it seems I must again



now before you post please try out your self
If you are talking about your response in post #9 to ipndrmath's assertion that "You can't switch on a string.", then it must be pointed out that your switch operates on an int, not a string. Granted, the value of the int depends on that of a string, but then we could have done without the switch and just used the if-else chain instead, which is a point behind ipndrmath's assertion.
__________________
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   Reply With Quote
Reply

Tags
c++, char, letter, word

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 07:40 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22