getline problem

This is a discussion on getline problem within the C++ Programming forums, part of the General Programming Boards category; I made a fortune teller and stock option option program and it works great. The only problem I have is ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    13

    getline problem

    I made a fortune teller and stock option option program and it works great. The only problem I have is with the getline command. When I enter my question in the fortune teller function it works fine but then I go to the leave function and it displays the answer without any input. I have tryed putting the cin.ignore() in the leave function but it still dosent work.


    Thank you for taking you time to read this.
    -Nick

    Code:
    //Workshop 7 Graded Project
    //Nick Waylett
    
    #include <iostream>
    #include <stdlib.h> 
    #include <time.h>
    #include <conio.h>
    #include <string>
    
    using namespace std;
    
    
    //prototype
    int menu();
    int fort();
    int stock();
    int leave();
    //prototype end
    
    ////////////////////////////////////////////////////////////////////////////////
    int main()//////////////////////////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    {
    //program
    
        menu();
      
    //program end
    getch();
    return 0;
    }
    ////////////////////////////////////////////////////////////////////////////////
    int menu()//Menu function//////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    {
    //system    
    system ("cls");
    //system end
    
    //vars
    int choice;
    //vars end
    
    //program
    
        //Menu
        cout<<"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"<<endl;
        cout<<"+ Welcome to Nicks Stock Market advice and Fortune Teller Program! +"<<endl;
        cout<<"=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"<<endl;
        cout<<"Select your choice."<<endl;
        cout<<"1. Stock Market Advice."<<endl;
        cout<<"2. Fortune Teller."<<endl;
        cout<<"3.Exit."<<endl;
        cout<<"Enter :";
        cin>>choice;
    
       //
            
        switch (choice)
    {
                    case 1:
                                    stock();
                                    break;
                    case 2:
                                    fort();
                                    break;
                    case 3:
                                    
                                    system("cls");
                                    cout<<"Good Bye!"<<endl;
                                    break;
                    default:
                            cout<<"Please enter a correct selection."<<endl;
                            getch();
                            main();
                            break;
                                    
    }
    //program end                                
    }
    ////////////////////////////////////////////////////////////////////////////////
    int fort()//fortune teller function/////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    {
    //system
    system("cls");
    //system end
    
    //variables    
    int number;
    string question;
    //variables end
                              srand((unsigned)time(NULL));//random number gen
                              number=1+rand()%6;//random number gen
    
    
    
           cout<<"Question about your future?: ";
           getline(cin,question);
           cin.ignore();
           
           
    
    
    
           if(number==1)
           {
                    cout<<"No way dude!"<<endl;
           }
           else if(number==2)
           {
                    cout<<"Most likely, yes."<<endl;
    
           }
           else if(number==3)
           {
                    cout<<"Try again tomorrow."<<endl;
    
           }
           else if(number==4)
           {
                    cout<<"Yes, I believe so!"<<endl;
    
           }
           else if(number==5)
           {
                    cout<<"I really dont know."<<endl;
    
           }
           else if(number==6)
           {
                    cout<<"Never!"<<endl;
    
           }
           else
           {
                    cout<<"Error in srand"<<endl;
           }
           getch();
           leave();
    //program end              
    }
    ////////////////////////////////////////////////////////////////////////////////
    int stock()//stocks function.///////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    {
    //system
    system ("cls");
    //system end
    
    //vars    
    int number;
    //vars end
             
    //program
          srand((unsigned)time(NULL));//random number
          number=1+rand()%3;//random number
    
                            switch(number)
                            {
                                 case 1:
                                 cout<<"You should definitely buy!"<<endl;
                                 break;
                                 case 2:
                                 cout<<"Sell! Sell! Sell!"<<endl;
                                 break;
                                 case 3:
                                 cout<<"Heck if I know. Go ask your stock broker."<<endl;
                                 break;
                                 default:
                                         cout<<"Error in srand"<<endl;
                                 break;
                            }
                            getch();
                            leave();
    //program end                                                          
    }
    ////////////////////////////////////////////////////////////////////////////////
    int leave()//The leave function/////////////////////////////////////////////////
    ////////////////////////////////////////////////////////////////////////////////
    {
    //colors
    system("cls");
    //colors end
       
    //variables    
    char yesorno;
    //variables end
    
    //program
    
             cout<<"Would you like to exit? (y or n):";
             cin>>yesorno;
             
             switch(yesorno)
             {
                            case 'y':
                                 cout<<"Press any key to exit."<<endl;
                                 system("cls");
                                 cout<<"Good Bye!"<<endl;
                                 break;
                           case 'n':
                                cout<<"Press any key."<<endl;
                                getch();
                                main();
                                break;
                           default:
                                   cout<<"Please enter the correct choice."<<endl;
                                   getch();
                                   system("cls");
                                   leave();
                                   break;
             }
    //program end
    }
    Last edited by Nick_365; 03-10-2006 at 08:31 PM.

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    13
    getline(function)?? I may have found the solution to my problem. I am sorry for the trouble and the double post.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 06:20 AM
  2. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 04:36 PM
  3. getline() problem
    By mrafcho001 in forum C++ Programming
    Replies: 5
    Last Post: 06-12-2005, 02:16 AM
  4. Need help with structures
    By yoursport in forum C++ Programming
    Replies: 8
    Last Post: 04-20-2005, 12:59 PM
  5. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 12:12 PM

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