Thread: variables problem

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    9

    variables problem

    ok before you post laughing just know that i am extreamly bad at c++ at the moment so dont laugh TOO hard

    anyway...

    i am trying a simple text base variable program in c++ and for some resone it only remembers numbers and not letters(allways says 2 when i type in letters)

    heres the code(note this is being compiled in dev c++)

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int name;
        cout << "what is your username?";
        cin >> name;
        cout << "hello " << name << ".";
        cout << endl;
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    any help?

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    would changing "int name;" to "char name;" work?

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    ok now it remembers letters but only one >_<(i.e. i type in "zeeky" and it says "hello z."[altho that dose work for nicknames XD])

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    That's because a character (char) is just that. A single character. "zeeky" is a string. If you're up for it, (and if you asked me, you're not quite there) you should look into std::strings. But before that you should continue with more basic programming like conditional statements and loops and what have you.

    Just don't criticize yourself by saying you're new and bad at programming. By the level of your program, it's obvious that you're new to programming and you're not bad at all. You're just simply new. Keep working and learning and you'll be fine.
    Sent from my iPad®

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    ok thanks

    right now i'm trying to make a program thats a simple text based "computer" that can do a few things

    heres a layout pic(note that this was made in ms paint quickly so it looks horrible)

    http://img364.imageshack.us/img364/6...mlayout3se.jpg

    one last question...

    how whold i go about the menu stull like

    if you type in (so and so here) then it gose to this part

    how whold i do that?

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    well, do you know how to make functions? the way i would od it was to have something that looks like this:

    A..............1
    B..............2
    C..............3

    Enter Choice: (cin>>choice)

    then ask some questions with loops...something like...

    if(choice=1)
    load the function a

    then do the loop for choices 2 and 3 and so on...

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    ok i whold be able to do the same thing with strings too right?

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    16
    i dont have too much expreience with strings, but i guess it would work...

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    cool

    let me try somthing.....

  10. #10
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    if(choice=1)
    Be careful about doing that--it's an error. That assigns 1 to choice instead of checking if choice is equal to 1. So, if choice equaled 3 prior to that line, choice would now equal 1. If you want to check if choice is equal to one, you have to use '==':
    Code:
    if(choice == 1)
    If you use '=' by accident, then choice will be assigned the value on the right. When you use '==', choice will be compared to the value on the right, and choice's value won't change. REMEMBER THIS. You will make that error many times.

    ok i whold be able to do the same thing with strings too right?
    If you use the C++ string type, you can treat strings similar to the way you treat numbers. For instance, you can assign a string to a string variable with '=', just like you can do with a numeric variable and a number. You can also add strings together with '+', and you can compare strings with '=='. Here is an example:
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	
    	string str = "hello";
    	str = str + " world";
    	cout<<str<<endl;
    
    	str = "goodbye";
    	cout<<str<<endl;
    
    	if(str == "goodbye")
    	{
    		cout<<"see ya"<<endl;
    	}
    		
    	return 0;
    }
    Last edited by 7stud; 03-08-2006 at 09:29 PM.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    oh what i have to do in dev c++ is replace return 0; with

    Code:
    system("PAUSE");
        return EXIT_SUCCESS;
    to get it to stay open

    oh and aint you sposed to say "goodbye" to get it to say "see ya" instead of the progy saying both?
    Last edited by zeeky; 03-08-2006 at 09:52 PM.

  12. #12
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    oh and aint you sposed to say "goodbye" to get it to say "see ya" instead of the progy saying both?
    It's just an example. You can make the program do whatever you want.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Posts
    9
    ok

    how whold i get it to do that?

  14. #14
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main()
    {
    	
    	cout<<"Type in 'hello' or 'goodbye': ";
    
    
    	string answer;
    	cin>>answer;
    	
    	if(answer == "hello")
    	{
    		cout<<"hi, pal"<<endl;
    	}
    	else if(answer == "goodbye")
    	{
    		cout<<"see, ya"<<endl;
    	}
    	else
    	{
    		cout<<"idiot!"<<endl;
    	}
    		
    	return 0;
    }

  15. #15
    Registered User
    Join Date
    Feb 2006
    Posts
    312

    Smile

    Quote Originally Posted by 7stud
    You will make that error many times.
    That's so true. I would add that if you want to minimise the number of times you make this mistake, get yourself into a habit of typing this instead (Requires a bit of reverse-logical thinking, which is why its a "habit" )
    Code:
     if (1 == choice)
    While this won't actually stop you typing '=' by accident, it will mean that the compiler screams at you for assignment to a constant - debugging the error becomes much easier.
    (It also helps if you can remember to use the 'const' keyword as often as possible, since in reality, "magic numbers" don't get used so often)
    Code:
    const int selection = 1;
    if ( selection == choice) {...}
    Last edited by Bench82; 03-09-2006 at 06:21 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. esbo's data sharing example
    By esbo in forum C Programming
    Replies: 49
    Last Post: 01-08-2008, 11:07 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Input File HELP, weird problem
    By gravity-1 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2005, 08:43 PM
  4. Replies: 6
    Last Post: 03-06-2005, 02:43 PM
  5. Replies: 4
    Last Post: 10-17-2002, 10:09 PM