make a char display a word or letter

This is a discussion on make a char display a word or letter within the C++ Programming forums, part of the General Programming Boards category; so i made this simple program to learn about structures and pointers and i declared 1 variable a char but ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    38

    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 ();
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    A character is just that: a character. If you want a bunch of characters, the data type you seek is "string".

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    ya but i couldnt even get it to do a character. and how do you do a string?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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;
    }

  5. #5
    beginner for now
    Join Date
    Oct 2009
    Posts
    35

    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

  6. #6
    Registered User
    Join Date
    Oct 2009
    Posts
    38
    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.

  7. #7
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    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

  8. #8
    Registered User
    Join Date
    Oct 2009
    Posts
    3

    Switch

    You can't switch on a string.

  9. #9
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    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

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?
    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?

  11. #11
    Registered User
    Join Date
    Apr 2008
    Location
    Australia
    Posts
    55
    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

  12. #12
    beginner for now
    Join Date
    Oct 2009
    Posts
    35
    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

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,462
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  4. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM

Tags for this Thread


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