Help! Please :(

This is a discussion on Help! Please :( within the C++ Programming forums, part of the General Programming Boards category; I want the user to type in a command then display a string but i keep getting my default error ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    15

    Help! Please :(

    I want the user to type in a command then display a string but i keep getting my default error messege.


    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <windows.h>
    using namespace std;
    
    string overview();
    string races();
    string world();
    
    int main()
    {
     char name[50];
     int input;
    
       cout << " Welcome to my test Proggy!" << endl;
       cout << " What is your name?" << endl;
       cin.getline(name, 50, '\n');
       cout << " Hello " << name;
       cout << endl << " Please choose one of the options below ";
       cout << endl;
       cout << " 1. Overview " << endl;
       cout << " 2. Races." << endl;
       cout << " 3. World." << endl;
       cin >> input;
    
      switch (input)
    {
       case 1: overview ();
    	   break;	 
       case 2: races ();
           break;
       case 3: world ();
    	   break;
    
      default: 	  
            cout<<"Error, bad input, quitting";	
    }
    
     system("pause");
    
     return 0;
    }
    
     string overview()
    {
       return " An Overview of DragonWars:";
    }
    
     string races()
    {
     return " An Overview of Races:";
    }
    
     string world()
    {
     return " An Overview of the world:";
    }

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Notice that your functions return a string, but there is no code to output that string. You either need to cout the string inside the function, or cout the string inside the case statement, or have a string variable in main that saves the return value of the functions and then use cout to print it after the switch statement.

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    15

    re

    Hmm....
    I tried return cout << "text"; but that gave me a bunch of errors. I'm so lost

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Your function can have more than one line. For example, in your function overview(), you can do:
    Code:
    string overview()
    {
        int i = 3;
        cout << i;
        string name = "Hi Mom!";
        cout << name;
        return name;
    }
    Notice how I did more than just return something. Don't copy that code, but make your functions cout the string you want first. Then have them return something if you want.

    Also note that you don't have to have your functions return a string, especially if you don't use it. This isn't necessary to get your program to work, but if you are able to get the cout to work inside the function, then try to change the return value from string to void, since it won't make sense for your functions to return something.

    Your program is fine except for this one detail. All you have to do is send the string you want to cout. No need to feel lost.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    Thanks so much for your help, I was able to compile without any error. However when i choose the option it displays the string but imeadetly i get an error pop up of an illegal operation thne ends the program.

    I've read something about return (r); Would this fix that illegal operation error?
    Thanks

  6. #6
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    I doubt that would help but you should post all your current code if you'd really like somebody to help figure it out.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    Oh, NM I figured it out. I declared my functions as void and removed the return values therefore it doest not cause any errors! Wow, programming is so fun lol!

  8. #8
    Registered User
    Join Date
    Oct 2003
    Posts
    15
    I did? ...LOL hehe.
    I solved my problem. Yay.
    However a new one has arised.
    I output a long string and it trails off into the right of the screen and i cant read it. I tried useing endl; but to no avail.

    Code:
    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <conio.h>
    #include <windows.h>
    using namespace std;
    
    void overview();
    void races();
    void world();
    
    int main()
    {
     char name[50];
     int input;
    
       cout << " Welcome to my test Proggy!" << endl;
       cout << " What is your name?" << endl;
       cin.getline(name, 50, '\n');
       cout << " Hello " << name;
       cout << endl << " Please choose one of the options below ";
       cout << endl;
       cout << " 1. Overview " << endl;
       cout << " 2. Races." << endl;
       cout << " 3. World." << endl;
       cin >> input;
    
      switch (input)
    {
       case 1: overview ();
    	   break;	 
       case 2: races ();
           break;
       case 3: world ();
    	   break;
    
      default: 	  
            cout<<"Error, bad input, quitting";	
    }
    
     system("pause");
    
     return 0;
    }
    
     void overview()
    {
       cout << " An Overview of DragonWars:" << endl;
       cout << " DragonWars (Not official) is a MultiPlayer RPG where";
       cout << " the player can choose between four Races: Draconians,";
       cout << " Humans, Elves and Drow.";
    
    }
    
     void races()
    {
       cout << " An Overview of the races";
    
    }
    
     void world()
    {
        cout << " An Overview of the World";
    
    }

  9. #9
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    You should use endl at the end of each line your writing to the console. If you don't it is put all on the same line. Putting it on several lines in the source file without using cout << endl; will not move down one line.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

Popular pages Recent additions subscribe to a feed

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