Need Help

This is a discussion on Need Help within the C++ Programming forums, part of the General Programming Boards category; Code: #include<conio.h> #include<iostream> using namespace std; int opcio1(); int opcio2(); void main() { char sel; do{ sel=getch(); }while((sel<'1' || sel>'2')&&sel!=27); ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    10

    Need Help

    Code:
    #include<conio.h>
    #include<iostream>
    using namespace std;
    
    
    int opcio1();
    int opcio2();
    
    
    
    void main()
    {
    
    	char sel;
    do{
    
            sel=getch();
    
        }while((sel<'1' || sel>'2')&&sel!=27);
    
    switch(sel){
    
            case '1':
    
                opcio1();
    
                break;
    
     
    
            case '2':
    
                opcio2();
    
                break;
    
    			case 27:
    
                return;
    
    }
    
    
    int opcio1()
    {
    
    	char sentence[100];
    	
    	cout<<"Ask what you should do in the market today!"<<endl;
    	cin.getline (sentence,100);
    	cin.ignore();
    	 int theNumber;
    		 theNumber = 1 + rand() % 3;		 
         switch (theNumber)  
    	 {        
    case 1:                        
         cout<<"You should definetly buy!"<<endl;                     
         break;         
    case 2:
         cout<<"Sell!Sell!Sell!"<<endl;
         break;
    case 3: 
         cout<<"Heck if I know. Go ask your brocker."<<endl;
         break;
    default:
         break; 
    }
    
         
    
    
    int opcio2()
    {
    	char sentence[100];
    cout<<"Do you feel lucky today!"<<endl;
    	cin.getline (sentence,100);
    	cin.ignore();
    	 int theNumbers;
    		 theNumbers = 1 + rand() % 4;
    		 if (theNumbers = 1)
    		 {
    			 cout<<"No way!"<<endl;
    		 }
    		 if (theNumbers = 2)
    		 {
    			 cout<<"Most Likely, yes!"<<endl;
    		 }
    		 if (theNumbers = 3)
    		 {
    			 cout<<"Try again tomorrow!"<<endl;
    		 }
    		 if (theNumbers = 4)
    		 {
    			 cout<<"Yes. I belive so!"<<endl;
    		 }
    
    }
    		 system("PAUSE");
         return 0;
    }
    Can somebody help me with this.

    At the beginning I have menu so the user can pick what function to enter.
    But I have errors with getch, and with the definition of functions. Can somebody help me

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,629
    Don't use void main! Use int main!
    Indent properly.
    Don't mix tabs and spaces. Use one.
    Tip: Try putting whitespace at locations to make it more readable.

    Code:
    #include<conio.h>
    #include<iostream>
    using namespace std;
    
    int opcio1();
    int opcio2();
    
    int main()
    {
    	char sel;
    	do
    	{
    		sel=getch();
    	}
    	while((sel < '1' || sel > '2') && sel != 27);
    	switch(sel)
    	{
    		case '1':
    			opcio1();
    			break;
    		case '2':
    			opcio2();
    			break;
    		case 27:
    	// Missing break;
    	// Missing }
    	return;
    }
    
    int opcio1()
    {
    	char sentence[100];
    	
    	cout<<"Ask what you should do in the market today!"<<endl;
    	cin.getline (sentence,100);
    	cin.ignore();
    	int theNumber;
    	theNumber = 1 + rand() &#37; 3;
    
    	switch (theNumber)  
    	{        
    		case 1:                        
    			cout<<"You should definetly buy!"<<endl;                     
    			break;
    
    		case 2:
    			cout<<"Sell!Sell!Sell!"<<endl;
    			break;
    		case 3:
    			cout<<"Heck if I know. Go ask your brocker."<<endl;
    			break;
    		default:
    			break; 
    	}
    // Missing }
    
    int opcio2()
    {
    	char sentence[100];
    	cout<<"Do you feel lucky today!"<<endl;
    	cin.getline (sentence,100);
    	cin.ignore();
    	int theNumbers;
    	theNumbers = 1 + rand() % 4;
    	// = is assign, == is compare, you can just as well use a switch.
    	if (theNumbers = 1)
    	{
    		cout<<"No way!"<<endl;
    	}
    	if (theNumbers = 2)
    	{
    		cout<<"Most Likely, yes!"<<endl;
    	}
    	if (theNumbers = 3)
    	{
    		cout<<"Try again tomorrow!"<<endl;
    	}
    	if (theNumbers = 4)
    	{
    		cout<<"Yes. I belive so!"<<endl;
    	}
    }
    
    // Placed at global level due to your indentation problems.
    	system("PAUSE");
    	return 0;
    }
    See, because you don't indent properly, you don't catch simple mistakes such as these.
    Read comments.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Jan 2008
    Posts
    10
    Thank you for your help.

    Here I made some improvements, but still I have small error:
    error C2146: syntax error : missing ';' before identifier 'cout'

    Here is the new version
    Code:
    #include<conio.h>
    #include<iostream>
    using namespace std;
    
    int opcio1();
    int opcio2();
    
    
     
    int main()
    {
      int choice
    cout << "==========================================" << endl;
    cout << " Menu" << endl;
    cout << "==========================================" << endl;
    cout << "1.Fortune Teller" << endl;
    cout << "2.Stock Market" << endl;
    cout << "==========================================" << endl;
     
    cout<<"For fortune teller press 1, for stock market press 2"<<endl;
    cin>>choice;
          if (choice == 1) opcio1();      
          else if (choice == 2) opcio2();      
          else cout << "Use only number shown in the menu" << endl;  
          
    
     
    system ("PAUSE");
    return 0;    
    }
    
    int opcio1()//Stock Market
    {
    	char sentence[100];	
    	cout<<"Ask what you should do in the market today!"<<endl;
    	cin.getline (sentence,100);
    	cin.ignore();
    	int theNumber;
    	theNumber = 1 + rand() % 3;
    
    	switch (theNumber)  
    	{        
    		case 1:                        
    			cout<<"You should definetly buy!"<<endl;                     
    			break;
    
    		case 2:
    			cout<<"Sell!Sell!Sell!"<<endl;
    			break;
    		case 3:
    			cout<<"Heck if I know. Go ask your brocker."<<endl;
    			break;
    		default:
    			break; 
    	}
    	system ("PAUSE");
    		return 0;
     }
    
    int opcio2()//Fortune teller
    {
    	char sentence[100];
    	cout<<"Do you feel lucky today!"<<endl;
    	cin.getline (sentence,100);
    	cin.ignore();
    	int theNumbers;
    	theNumbers = 1 + rand() % 4;
    	// = is assign, == is compare, you can just as well use a switch.
    	if (theNumbers == 1)
    	{
    		cout<<"No way!"<<endl;
    	}
    	else if (theNumbers == 2)
    	{
    		cout<<"Most Likely, yes!"<<endl;
    	}
    	else if (theNumbers ==3)
    	{
    		cout<<"Try again tomorrow!"<<endl;
    	}
    	else if (theNumbers == 4)
    	{
    		cout<<"Yes. I belive so!"<<endl;
    	}
    	system ("PAUSE");
    		return 0;
    }
    And I have one more question, after the program is done, how can I make a menu which can ask the user
    Do you like to exit OR
    Do you like to go back in the beginning?

    Thank You

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    int choice;
    Note that error messages include line numbers, which would help greatly in finding such a thing. (In this case, the line number would have been for the line with the cout while the ; is missing on the line above, but since it tells you "before" that's easy to spot.

    If you want things to happen over and over and over again, you will need to put the things that happen over and over and over again inside a loop structure. (You know how to put a menu on the screen and ask for input, you've got it in your program already.)

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,629
    You should fix your indentation first. Did my reply fixing your indentation teach you nothing?
    Use a loop to get your desired results, AFTER your fix the indentation.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    10
    Thank you gays!!

    Finally I dont have any errors in my script.

    But I dont know why does not go back to main in opcio3()

    I need to get back in main() and to run the script as many times as the user want to.

    Any suggestions

    Code:
    #include<conio.h>
    #include<iostream>
    #include <cstdlib>
    #include <stdlib.h>
    using namespace std;
    
    int opcio1();
    int opcio2();
    int opcio3();
    
    
     
    int main()
    {
      int choice;
    cout << "==========================================" << endl;
    cout << " Menu" << endl;
    cout << "==========================================" << endl;
    cout << "1.Fortune Teller" << endl;
    cout << "2.Stock Market" << endl;
    cout << "==========================================" << endl;
     
    cout<<"For fortune teller press 1, for stock market press 2"<<endl;
    cin>>choice;
          if (choice == 1) opcio1();      
          else if (choice == 2) opcio2();      
          else cout << "Use only number shown in the menu" << endl;  
          
    
      
    }
    
    int opcio1()//Stock Market
    {
    	char sentence[100];	
    	cout<<"Ask what you should do in the market today!"<<endl;
    	cin.getline (sentence,100);
    	cin.ignore();
    	int theNumber;
    	theNumber = 1 + rand() % 3;
    			
    	switch (theNumber)  
    	{        
    		case 1:                        
    			cout<<"You should definetly buy!"<<endl;                     
    			break;
    
    		case 2:
    			cout<<"Sell!Sell!Sell!"<<endl;
    			break;
    		case 3:
    			cout<<"Heck if I know. Go ask your brocker."<<endl;
    			break;
    		default:
    			break; 
    	}
    			 
    		system("cls");
    		return opcio3();
     }
    
    int opcio2()//Fortune teller
    {
    	char sentence[100];
    	cout<<"Do you feel lucky today!"<<endl;
    	cin.getline (sentence,100);
    	cin.ignore();
    	int theNumbers;
    	theNumbers = 1 + rand() % 4;
    	
    	if (theNumbers == 1)
    	{
    		cout<<"No way!"<<endl;
    	}
    	else if (theNumbers == 2)
    	{
    		cout<<"Most Likely, yes!"<<endl;
    	}
    	else if (theNumbers ==3)
    	{
    		cout<<"Try again tomorrow!"<<endl;
    	}
    	else if (theNumbers == 4)
    	{
    		cout<<"Yes. I belive so!"<<endl;
    	}
    
    	system("cls");
    		return opcio3();
    }
    int opcio3()
    {
    int choice1;
    
    cout<<"If you want to exit press 1, for starting over press 2"<<endl;
    cin>>choice1;
          	       
          if (choice1 == 1) exit(-1);
    	  else if(choice1 == 2) main(); 
    	  else cout << "Use only number shown in the menu" << endl;
    	  	
    }

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Add a loop to main that contains all the code you want to repeat, then add a way to exit that loop and end the program.

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    10
    I allready have a way to exit the program.
    I need the script to repeat indefinetly until the user want to exit.

    Do you have any idea how to create the loop.

    Please, I need this scipt for tomorrow.

  9. #9
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I would get rid of opcio3() and move it's logic up to main(). Then, make the 3rd choice in main() for the user to exit. Finally, wrap the logic you want repeating in main() inside a while(1) block, break if the choice is 3.

    Todd

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,629
    You're not listening. What am I wasting my time on you?

    You should fix your indentation first. Did my reply fixing your indentation teach you nothing?
    Either you acknowledge you are trying to do it or you don't, but don't ignore it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    10
    Be specific.

    What is wrong with my script????

    I've started learning C++ 2 months ago, so I'm not an expert.
    Plus, my native language is not English.

    Thank you. I know that you are trying to help me, but please be more specific.

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,629
    OK, it's good that you're listening. To make code readable, you need to indent it properly so we know what code goes where.
    For every {, the rule says +1 indent. So all code inside ifs, while, switch, etc, should be indented so we know what code belongs where.
    It also helps YOU find problems, as I mentioned above. All those what code belongs where-problem could have been avoided with proper indentation.
    I showed you how to indent the code in the first reply. It's not hard, try it and see.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

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