Thread: Um help plez

  1. #1
    Registered User mill1000's Avatar
    Join Date
    Nov 2001
    Posts
    43

    Unhappy Um help plez

    i cant get this code to loop back to the main() fuction. some time it does but not others




    >>>>>>>>>>>>>>>>>>>>>Code<<<<<<<<<<<<<<<<<<<<<<

    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream.h>
    #include <conio.h>
    int BYC();
    int TDC();

    int main()
    {
    int sel;
    while (sel>4 || sel<1)
    {
    cout<<"1. Bet You Can't\n";
    cout<<"2. Two Digit Calculater\n";
    cout<<"3. \n";
    cout<<"4. Exit\n";
    cout<<"Enter selection 1, 2, 3 or 4: ";
    cin>>sel;
    if (sel>4 || sel<1)
    {
    cout<<"Error! No such input.\n";
    }
    else
    {
    }
    switch (sel)
    {
    case 1:
    BYC();
    break;
    case 2:
    TDC();
    break;
    case 3:
    break;
    case 4:
    break;
    }
    }
    return 0;
    }

    int BYC()
    {
    char let;
    while(let!='l')
    {
    for(int num=1;num<1000;num++)
    {
    cout<<"Bet You Can't Stop This!\n";
    }
    cout<<"I'll give you a chance to stop it(enter a letter):";
    cin>>let;
    }
    cout<<"Lucky guess\n\n";
    main();
    return 0;
    }

    int TDC()
    {
    int n1;
    char op;
    int n2;
    cout<<"This calculater can only calculater two intergers, * = Multiplycation,\n / = Division, + = Addition, - = Subtraction.\n";
    cout<<"Enter first integer:";
    cin>>n1;
    cout<<"Enter a operator:";
    cin>>op;
    cout<<"Enter second integer:";
    cin>>n2;
    switch(op)
    {
    case '+':
    cout<<n1+n2;
    break;
    case '-':
    cout<<n1-n2;
    break;
    case '*':
    cout<<n1*n2;
    break;
    case '/':
    cout<<n1/n2;
    break;
    }
    main();
    return 0;
    }

  2. #2
    Unregistered
    Guest
    Cant read it until you use code tags, check out one of the sticky threads at the top of the page. [ code ] and [ /code ] are ESSENTIAL!!

  3. #3
    Registered User blight2c's Avatar
    Join Date
    Mar 2002
    Posts
    266
    do you mean return main() after it runs the functions? if so, it does return to main(). the way you had the while loop written there was nothing to keep the flow inside. i made the noted change and also took out the "main() return(0);" you had at the bottom of each function--i don't know if they served a purpose or not

    also, i used code tags. it makes your code easier to read so use them next time.

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <iostream.h> 
    #include <conio.h> 
    
    int BYC(); 
    int TDC(); 
    
    int main() 
    { 
    	int sel; 
    	while (sel!=4)// :cool: 
    	{ 
    		cout<<"1. Bet You Can't\n"; 
    		cout<<"2. Two Digit Calculater\n"; 
    		cout<<"3. \n"; 
    		cout<<"4. Exit\n"; 
    		cout<<"Enter selection 1, 2, 3 or 4: "; 
    		cin>>sel; 
    		if (sel>4 || sel<1) 
    			{ 
    			cout<<"Error! No such input.\n"; 
    			} 
    
    		switch (sel) 
    		{ 
    			case 1: 
    					BYC(); 
    					break; 
    			case 2: 
    					TDC(); 
    					break; 
    			case 3: 
    					break; 
    			case 4: 
    					break; 
    		} 
    	} 
    	return 0; 
    } 
    
    int BYC() 
    { 
    	char let; 
    	while(let!='l') 
    	{ 
    		for(int num=1;num<1000;num++) 
    		{ 
    			cout<<"Bet You Can't Stop This!\n"; 
    		} 
    		cout<<"I'll give you a chance to stop it(enter a letter):"; 
    		cin>>let; 
    	} 
    		cout<<"Lucky guess\n\n"; 
    }
    
    int TDC() 
    { 
    	int n1; 
    	char op; 
    	int n2; 
    	cout<<"This calculater can only calculater two intergers, \n" 
    		<<"* = Multiplycation,\n / = Division, + = Addition, - = Subtraction.\n"; 
    	cout<<"Enter first integer:"; 
    	cin>>n1; 
    	cout<<"Enter a operator:"; 
    	cin>>op; 
    	cout<<"Enter second integer:"; 
    	cin>>n2; 
    	switch(op) 
    	{ 
    		case '+': 
    					cout<<n1+n2; 
    					break; 
    		case '-': 
    					cout<<n1-n2; 
    					break; 
    		case '*': 
    					cout<<n1*n2; 
    					break; 
    		case '/': 
    					cout<<n1/n2; 
    					break; 
    	} 
     
    }

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Don't call the main function, in C++ the result of calling main is undefined. If it works at all then bad things will usually follow.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Also, you should probably initialize sel to NULL before you use it...Bad things can happen in more complex code
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Having trouble with, um, Class setup?
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 01-24-2006, 10:07 PM
  2. um canīt understand why this code isnīt working...
    By Shogun in forum C Programming
    Replies: 9
    Last Post: 04-25-2003, 09:38 AM
  3. Replies: 5
    Last Post: 03-11-2002, 05:22 PM
  4. Um Position Cursor
    By Stealth in forum C++ Programming
    Replies: 4
    Last Post: 10-19-2001, 07:09 PM