C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-10-2006, 08:27 PM   #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.
Nick_365 is offline   Reply With Quote
Old 03-10-2006, 08:36 PM   #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.
Nick_365 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Bin packing problem.... 81N4RY_DR460N C++ Programming 0 08-01-2005 05:20 AM
Words and lines count problem emo C Programming 1 07-12-2005 03:36 PM
getline() problem mrafcho001 C++ Programming 5 06-12-2005 01:16 AM
Need help with structures yoursport C++ Programming 8 04-20-2005 11:59 AM
getline help ProjectsProject C++ Programming 3 06-14-2004 11:12 AM


All times are GMT -6. The time now is 01:41 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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