Thread: how: loop program..

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    29

    how: loop program..

    how can i make the program loop instead of "press any key to continue"?
    my calculator will not do this (it says main() is unidentified) in vc++

    Code:
    #include <iostream.h>
    #include <conio.h>
    
    int ad()
    {
    cout<<"Enter two numbers to add:\n";
    int x,y;
    cin>>x;
    cin>>y;
    cout<<"="<<x+y<<"\n";
    return 0;
    }
    
    int subt()
    {
    	cout<<"Enter two numbers to subtract:\n";
    	int x,y;
    	cin>>x;
    	cin>>y;
    	cout<<"="<<x-y<<"\n";
    return 0;
    }
    
    int divi()
    {
    	cout<<"Enter two numbers to divide:\n";
    	int x,y;
    	cin>>x;
    	cin>>y;
    	cout<<"="<<x/y<<"\n";
    return 0;
    }
    
    int multi()
    {
    	cout<<"Enter two numbers to multiply:\n";
    	int x,y;
    	cin>>x;
    	cin>>y;
    	cout<<"="<<x*y<<"\n";
    return 0;
    }
    
    int main()
    {
    	cout<<"1: add\n";
    	cout<<"2: subtract\n";
    	cout<<"3: multiply\n";
    	cout<<"4: divide\n";
    	int input;
    	cin>>input;
    	switch (input)
    	{
    case 1: ad();
    break;
    
    case 2: subt();
    break;
    
    case 3: multi();
    break;
    
    case 4: divi();
    break;
    
    default: main();
    	}
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    11
    Put an exit value at the menu, and then put a 'do..while' loop around the code.

    So say if exit is case 5...

    Code:
    Do {
    
    *Your code here*
    
    }while(input != 5);

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    no man...
    see these bits (subroutines maybe)
    Code:
    int subt()
    {
    	cout<<"Enter two numbers to subtract:\n";
    	int x,y;
    	cin>>x;
    	cin>>y;
    	cout<<"="<<x-y<<"\n";
    return 0;
    i want it to return to main() after those (preferably with the screen blanked first). but the compiler will not work, cos main() is defined after those subroutines..
    any idea?
    cheers

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    Originally posted by pico
    no man...
    see these bits (subroutines maybe)
    Code:
    int subt()
    {
    	cout<<"Enter two numbers to subtract:\n";
    	int x,y;
    	cin>>x;
    	cin>>y;
    	cout<<"="<<x-y<<"\n";
    return 0;
    i want it to return to main() after those (preferably with the screen blanked first). but the compiler will not work, cos main() is defined after those subroutines..
    any idea?
    cheers
    really your function doesnt need at all to be a returned function it would be much better if its void function

    anyway what do u want to return to main() after those (preferably with the screen blanked first).?????
    sorry what do you want to return to main fn

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    and it would be much better if you defined your variables x,y in the main function where passing them by value to add,sub,mul .and div functions

    I think it would be much better

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    ok.
    the program asks you to choose:
    1.add
    2.subtract
    3.divide
    4. multiply

    you then asked for two numbers, operation is carried out and result returned.
    after this, i want the program to return to the beggining and ask the user to choose:
    1.add
    2.subtract
    3.divide
    4. multiply
    as if the program got run again.
    but as with all programs, i get "press any key to continue" then the app exits.
    do you understand?
    thanks

  7. #7
    Registered User
    Join Date
    Mar 2004
    Posts
    18
    Couldn't you make use of the for() loop?



    To make an infinite loop, use For(;;)


    You could put your menu in that, and when the user wanted to exit, you could have something like,
    Code:
    if(input == 0) break ;

    Also, don't have your functions return 0.

    You could declare them as void.
    Last edited by CSmith; 03-07-2004 at 01:34 PM.

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    Originally posted by pico
    ok.
    the program asks you to choose:
    1.add
    2.subtract
    3.divide
    4. multiply

    you then asked for two numbers, operation is carried out and result returned.
    after this, i want the program to return to the beggining and ask the user to choose:
    1.add
    2.subtract
    3.divide
    4. multiply
    as if the program got run again.
    but as with all programs, i get "press any key to continue" then the app exits.
    do you understand?
    thanks
    really for the console when the message "press any key to continue" after executing the program that means that there is no looping ... i.e the program ends

    about that your problem i will try to write a code for your program i hope it works

    and i will write it and send you soon God willing
    Last edited by M_Ghani; 03-07-2004 at 02:30 PM.

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    I wrote this code and check it on my compiler and it works

    Code:
    #include<iostream>
    #include<cstdlib>   //for exit
    using namespace std;
    int main()
    {
    	double x,y;
    	char op,ch; //op for type of operator and ch is the character you enter to stop looping
    	do
    	{
    		cout<<"enter the mathematical equation you want :\n"; //for example : 3 + 8 and click enter
    		cin>>x;
    		cin>>op;
    		cin>>y;
    		switch(op)
    		{
    		case '+':
    			cout<<"result ="<<x+y<<endl;
    			break;
    		case '-':
    			cout<<"result ="<<x-y<<endl;
    			break;
    		case '*':
    			cout<<"result = "<<x*y<<endl;
    			break;
    		case '/':
    			cout<<"result = "<<x/y<<endl;
    			break;
    		default:
    			cout<<"error in entry \n";
    		}
    		
    		cout<<"enter n to exit or enter other letter to continue :\n";
    		cin>>ch;
    	}
    	while(!(ch=='n')||(ch=='N'));
    	exit(0);
    	return 0;
    }
    I hope it works with you

  10. #10
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    as you really dont need that all functions you wrote for each operation and it can be easily solved using operator variable

    anyway if you want any modification on my code tell me coz it might not meet your needs

    anyway i hope it works

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    29
    thanks mate.

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Code:
    while(!(ch=='n')||(ch=='N'));
    there's a problem here... it should be:
    Code:
    while(!((ch=='n')||(ch=='N')));
    //or
    while(ch!='n'||ch!='N'); //or some variation
    the way you did it, if the user enters 'N', the program will run again... it's not the end of the world, but it's not right, either...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    yes you are right major_small .. i didnt take care of that

    thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help, program stuck in a while loop
    By DSman in forum C Programming
    Replies: 8
    Last Post: 09-24-2007, 01:28 PM
  2. Program skipping while loop
    By JFonseka in forum C Programming
    Replies: 9
    Last Post: 08-09-2007, 03:30 AM
  3. detect infinite loop in c program
    By abhivyakat in forum C Programming
    Replies: 19
    Last Post: 10-01-2003, 06:55 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. Need Help on Program Using For Loop
    By Unregistered in forum C++ Programming
    Replies: 10
    Last Post: 02-26-2002, 06:54 PM