Jumping into C++ problems

This is a discussion on Jumping into C++ problems within the C++ Programming forums, part of the General Programming Boards category; Hi,, I am trying to solve the problem "Write a menu program that lets the user select from a list ...

  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
    21,749
    That is because choices() returns a string, but you did not actually print the string that was returned.
    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

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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
    21,749
    Did you do:
    Code:
    cout << choices();
    ?
    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

  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
    407
    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 02:36 AM.

  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, 02:47 AM
  5. Jumping Script
    By Krak in forum Game Programming
    Replies: 5
    Last Post: 01-12-2004, 02:19 PM

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