CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 32
  1. #1
    Join Date
    Feb 2009
    Posts
    11

    Someone please help me out!!

    Ok, I started learning the basics today, and I've run into a small problem. I spent hours looking for this on the internet, but I can't seem to find anything helpful.

    The book I'm using is great as far as explaining the basic structure, and functions of programs, but It only uses numbers...

    simply put:
    I need to know how to create a variable that stores text that has been input by the user.

    If someone could heplp me out on this one, I'm sure I can take it from there

    Ty!

  2. #2
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: Someone please help me out!!

    Wrong forum. This is a non-visual C++ issue, so you should have posted it here.

    If your book doesn't show you how to get and store input as a string, it was probably never worth having in the first place... out of curiosity, what is the title of the book?

    As for your question, start here.

    [ NOTE - ovidiucucu ]
    I just have merged the two threads.
    Last edited by ovidiucucu; February 4th, 2009 at 03:40 AM.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  3. #3
    Join Date
    Feb 2009
    Posts
    11

    Please help! [Variables and text input]

    I've searched the internet for tutorials on chars and strings that could help me do the following:

    Prompt for input of a word, and, storing that word in a variable, use the variable to execute a certain command.

    I need a very clear and thorough explanation! ( it might help if you try explaining in a way that you would to someone who can't even read ;D )

    Any and all help will be greatly appreciated. Ty ;]

  4. #4
    Join Date
    Aug 2007
    Posts
    858

    Re: Please help! [Variables and text input]

    A basic example would be:

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main( )
    {
      string input;
      
      do
      {
        cout << "Enter some text (\"exit\" to end):" << endl;
        getline(cin, input);
        cout << "You entered: " << input << endl;
      } while (!cin.fail( ) && (input != "exit"));
      
      return 0;
    }
    If you want something more than that, you're going to have to explain what you want in more detail.

  5. #5
    Join Date
    Feb 2009
    Posts
    11

    Re: Please help! [Variables and text input]

    Sorry about the bump, but I thought if I include what I've got so far it might help ppl to help me ;D

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int num1;
    int num2;
    int num3;
    
    using namespace std;
    
    
    #include <string>
    
    using std::string;
    
    
    int main() {
    
            start:	
    
    	cout << "Please type a number\n" << endl;
    
        cin >> num1;
    
    	cout << "Please type another number\n" << endl;
    	
    	cin >> num2;
    
        num3 = num1 + num2;
    
    	cout << "The sum is " << num3 << endl;
    
            back:
    
    	cout << "Would you like to continue?\n" << endl;
    
    	string answer; //Creates the string named "answer".
    
    	getline (cin, answer); //Adds whatever the user typed to the newly created string.
    
    	if (answer == "yes") {  // Checks if what was typed was "yes".
    
               string answer = ""; //Cleans the string, so that it won't cause an infinite loop.
               
               goto start; //Returns to the beggining of the code.   
           
               else if (answer == "no") { //Checks if the user typed "no".
    
                  string answer = ""; //Cleans the string...
    
                  goto finish; //Goes to the end of the code.
                  
                  else
                     
                     cout << "Sorry, I didn't understand that.\n"
    
                     goto back; //Goes back to the prompt for continuance phase.
              }
          }
           	
    finish:
    return 0;
    }
    Last edited by ovidiucucu; February 4th, 2009 at 03:39 AM. Reason: addded [CODE] tags

  6. #6
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Please help! [Variables and text input]

    Please post your well indented code in [code][/code] bbcode tags.

    Now, the first thing that jumps out at me is the very unnecessary use of goto in your code. You should use a loop instead. Another bad habit that you may develop is the unnecessary use of global variables. It does not matter in this case since you only have the main function, but you should make them local variables anyway to develop a good habit.

    Other potential bad habits include the using directive (using namespace std) before an include. If you do use using directives, they should come after all your includes. It is also rather strange that you have both a using directive and using declarations for names in that namespace (i.e., std::cout, std::cin, std::endl and std::string).

    Right. Is there any specific problem with your program? Like, if it fails to compile, you should state the error messages. If it does not run as expected, you should tell us how does it not work.
    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
    Kindly rate my posts if you found them useful

  7. #7
    Join Date
    Jan 2009
    Posts
    24

    Re: Please help! [Variables and text input]

    you need functions and while loops!
    goto is a bad habit because it ends up with messy code, which is probably messing up your code.

  8. #8
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: Someone please help me out!!

    [ Merged threads ]

    @To Gabiru: Please use [CODE] tags to make source code more readable!
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Please help! [Variables and text input]

    Code:
               
               goto start; //Returns to the beggining of the code.   
    //...
               goto finish; //Goes to the end of the code.
    //...
               goto back; //Goes back to the prompt for continuance phase.
    http://en.wikipedia.org/wiki/Spaghetti_code

    Please get rid of the gotos.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Feb 2009
    Posts
    11

    Re: Someone please help me out!!

    Thanks for replying ;]

    Ok, I understand that my code may be "spaghetti"-like, and I'll do my best to fix that, but for me to do so, I'll need some pointers. I assume that when you say: "remove the goto's", you mean :"remove the goto's and code something more efficient to replace them" ... so what can I work with here?

    Although the code is using the goto's, I didn't leave out any blank spots.. and it IS a rather simple code, so it bugs me that it's not working. I made a few adjustments, and here is what I was at:

    Code:
    #include <iostream>
    
    using std::cout;
    using std::cin;
    using std::endl;
    
    int num1;
    int num2;
    int num3;
    
    using namespace std;
    
    
    #include <string>
    
    using std::string;
    
    
    int main() {
    
        start:	
    
    	cout << "Please type a number\n" << endl;
    
        cin >> num1;
    
    	cout << "Please type another number\n" << endl;
    	
    	cin >> num2;
    
        num3 = num1 + num2;
    
    	cout << "The sum is " << num3 << endl;
    
        back:
    
    	cout << "Would you like to continue?\n" << endl;
    
    	string answer; //Creates the string named "answer".
    
    	getline (cin, answer); //Adds whatever the user typed to the newly created string.
    
    	if (answer == "yes") {  // Checks if what was typed was "yes".
    
           string answer = ""; //Cleans the string, so that it won't cause an infinite loop.
               
           goto start; //Returns to the beggining of the code.
    	}
           
    	if (answer == "no") { //Checks if the user typed "no".
    
           string answer = ""; //Cleans the string...
    
    	}
    	    
    	if ((answer != "yes") && (answer != "no")) { 
                     
           cout << "Sorry, I didn't understand that.\n";
    
    			  
    		   
        } 
                  
     
           	
    return 0;
    }
    What happens is that after entering in the first and second numbers, it gives me the result, and then asks me if I want to continue, but nomater what I type(yes/no/haha/huhu...) it executes the display message intended for the condition of the answer string not being yes or no(the last if statement). And lastly, when you do type something, it just ends the code, saying to press any key to continue, so it'll close.

    Before I was getting an infinite loop error, and I thought it might have been due to the way I made the string clear, but when I made the last adjustment, taking out the else's and cleaning up the if's, the loop problem got solved.

    Ty for the help ;]

  11. #11
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Someone please help me out!!

    Quote Originally Posted by Gabiru View Post
    Thanks for replying ;]

    Ok, I understand that my code may be "spaghetti"-like, and I'll do my best to fix that, but for me to do so, I'll need some pointers. I assume that when you say: "remove the goto's", you mean :"remove the goto's and code something more efficient to replace them" ... so what can I work with here?
    It's not a question of efficiency. Since gotos are closer in meaning to what the final assembly code will do after compilation, strictly speaking they might be more efficient than the alternative (very slightly).

    It's a question of readability and maintainability. If you see a "goto" statement in a program that you don't know inside and out, you have to search all over to find the label it's jumping to. Very difficult to understand at a glance what is going on.

    You should structure your code using loops and if statements entirely rather than gotos. That way, when you come to the end of a loop or even a break or continue statement, it's immediately clear where the code will be going next----either right after the loop block if the loop is over, or to the beginning of it.

  12. #12
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Someone please help me out!!

    Quote Originally Posted by Gabiru
    Ok, I understand that my code may be "spaghetti"-like, and I'll do my best to fix that, but for me to do so, I'll need some pointers. I assume that when you say: "remove the goto's", you mean :"remove the goto's and code something more efficient to replace them" ... so what can I work with here?
    As I suggested, you can use a loop. In conjunction with the loop you can write and use functions as E-man96 suggested, though helper functions are probably not very useful at this point.

    Quote Originally Posted by Gabiru
    What happens is that after entering in the first and second numbers, it gives me the result, and then asks me if I want to continue, but nomater what I type(yes/no/haha/huhu...) it executes the display message intended for the condition of the answer string not being yes or no(the last if statement).
    One possible reason is that this defines a new variable named answer in a more local scope:
    Code:
    string answer = ""; //Cleans the string, so that it won't cause an infinite loop.
    You probably intended to write:
    Code:
    answer = "";
    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
    Kindly rate my posts if you found them useful

  13. #13
    Join Date
    Feb 2009
    Posts
    11

    Re: Someone please help me out!!

    Ok, replacing the goto's for while loop. But I think the problem is the string... the program doesn't seem to be injecting the user input into the string variable =(, any ideas?

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Someone please help me out!!

    Quote Originally Posted by Gabiru
    But I think the problem is the string... the program doesn't seem to be injecting the user input into the string variable =(, any ideas?
    As in you applied the fix that I suggested and the problem persists? Time to use a debugger, methinks.
    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
    Kindly rate my posts if you found them useful

  15. #15
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Someone please help me out!!

    This is the getline() problem that's been addressed in about 3 threads in the last few days. Long story short: Try putting cin.ignore(some large number,'\n') in front of the getline().

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured