Thread: Jumping into C++ problems

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    114

    Jumping into C++ problems

    Hi,, I am trying to solve the problem "Write a menu program that lets the user select from a list of options, and if the input is not oneof the options, reprint the list" using function calls. This is my code
    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    string choices()
    {
        return "what do u want?? tea or coffee\n press 1 for tea and 2 for cofee";
    
    
    }
    
    
    int main()
    {
        choices();
        string input;
        cin>>input;
       while(true)
       {
           if (input=="1"||input=="2")
           {   cout<<"please wait";
               break;
           }
           else
           {
               cout<<"wrong options insert again";
               choices();
               cin>>input;
           }
    
    
    
    
    
    
       }
    
    
    
    
    }
    problem is,though it compiles fine, the function "choices" never give the cout, no matter how many times I call it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That is because choices() returns a string, but you did not actually print the string that was returned.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps replace line 6 with a cout instead of a return

    Or when you call it, do
    cout << choices();
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by Salem View Post
    Perhaps replace line 6 with a cout instead of a return

    Or when you call it, do
    cout << choices();
    But,I tried that before but than it gives an error regarding no return statement,When I inserted "return 0;"it works. but should I do this?? as it is a string fuunction, why should I return an integer "0"? By the way, I like the 2nd option.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Did you do:
    Code:
    cout << choices();
    ?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by laserlight View Post
    That is because choices() returns a string, but you did not actually print the string that was returned.
    Thanx, that helped.

  7. #7
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    But,I tried that before but than it gives an error regarding no return statement,When I inserted "return 0;"it works. but should I do this?? as it is a string fuunction, why should I return an integer "0"? By the way, I like the 2nd option.
    This makes it sound like you are confused about function declaration (when you precede the function name with a type), and function definitions (when you tell the compiler what the function should do).

    You get to declare what TYPE any function is. If you don't wany it to return anything look into the void type. If you want it to return something then return the appropriate data type that you want returned. Just like declaring any variable you can also declare any function in the same manner. You just have to make sure that said function has a return of the type of which you declared it. IE.

    Code:
     
    int ThisFunction(){
      int a = 10; // just for example purposes
      return a; // the function must return a variable that matches it's type.
    }
    In this manner you can also create any type of function. It can be
    Code:
    int ThisFunction(){
      int Aint = 10;
      return Aint;
    }
    string ThisFunction(){
      string Astring = "some arbitrary value";
      return Astring;
    } // as long as you #include <string>
    float ThisFunction(){
      float Afloat = 3.14;
      return Afloat;
    }
    double ThisFunction(){
      double Adouble = 0.0012353;
      return Adouble;
    }
    On top of that your functions can actually be declared as and return structures and classes that you defined as well. I hope I didn't confuse you, as this is an extremely important aspect to programming in C++.
    Last edited by Lesshardtofind; 01-11-2013 at 03:36 AM.
    Virtual reality hello world http://www.rodneybrothers.com/vr/vrh...rld/index.html in html and javascript.
    Viewable with dodocase, google cardboard, OR, and other compatible VR gear.

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Thank you very much. I didnt knew that void functions dont need any return type. But, I still have to practise a lot similar type of problems to get a clear idea. can you give me a link where I can find this types of problems???

  9. #9
    Registered User
    Join Date
    Jan 2013
    Posts
    114
    Quote Originally Posted by laserlight View Post
    Did you do:
    Code:
    cout << choices();
    ?
    yes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. character jumping
    By colourfulbanana in forum Game Programming
    Replies: 2
    Last Post: 06-30-2012, 05:37 PM
  2. Jumping to OOP in C++ from C
    By meadhikari in forum C++ Programming
    Replies: 6
    Last Post: 07-16-2010, 05:26 AM
  3. Jumping in the code...
    By AdampqmabA in forum C++ Programming
    Replies: 24
    Last Post: 10-06-2008, 05:34 PM
  4. Jumping between functions
    By Nexus-ZERO in forum C Programming
    Replies: 8
    Last Post: 01-14-2008, 03:47 AM
  5. Jumping Script
    By Krak in forum Game Programming
    Replies: 5
    Last Post: 01-12-2004, 03:19 PM