Thread: need help with code

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    need help with code

    i cannot figure out why my code is not working. i have tried everything that i could read or watch. as this is a home work ass please do not modify it for me but just point me in the right directio with suggestions, thanks

    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    
    int main()
    {
    
    char letter; // users menu choice
    int num; // the number of numbers that the user wants to enter
    int num1; // user inputed variables
    int ans; // largest variable
    int answ; // smallest variable
    
    cout << " Please type in your choice by entering a b or c " << endl;
    cout << " a= Find the largest number with a known quantity of whole two digit numbers " << endl;
    cout << " b= Find the smallest number with an unknown quantity of numbers " << endl;
    cout << " c= Quit " << endl;
    
    cin >> letter;
    
    if (letter == 'a')
    {
     cout << "How many numbers would you like to enter? " << endl;
     cin >> num;
    
     cout << "Please enter a number and press enter. " << endl;
     cout << endl;
    
     ans = 0;
    
    int count; // declaring loop control variable
    
    for (count = 0; count < num; count = count++)
    {
    
    cin >> num1;
    ans = num1;
    cin >> num1;
    
    if (num1 > ans)
    {
    ans = num1;
    cin >> num1;
    }
     
    else if (ans > num1)
    {
    ans = ans;
    cin >> num1;
    }
    }
    
    cout << "The Largest number you entered was " << ans << endl;
    cout << endl;
    
    return main();
    }
    
    else if (letter == 'b')
    {
    	cout << " Enter as many numbers as you like, when you are finished enter '99' " << endl;
     cout << endl;
    
    answ = 0;
    
    while (answ != 99)
    {
    cin >> num1;
    answ = num1;
    cin >> num1;
    
    
    if (num1 < answ)
    {
    answ = num1;
    cin >> num1;
    }
     
    else if (answ < num1)
    {
    answ = ans;
    cin >> num1;
    }
    }
    
    cout << "The smallest number you entered was " << answ << endl;
    cout << endl;
    }
    
    
    system("pause");
    
    return 0;
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    109
    What happens? Also, try indenting your code properly. It's difficult to read, otherwise.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    8
    What part is failing for you?

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    8
    easier to read...
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    
    int main()
    {
    
    char letter; // users menu choice
    int num; // the number of numbers that the user wants to enter
    int num1; // user inputed variables
    int ans; // largest variable
    int answ; // smallest variable
    
    cout << " Please type in your choice by entering a b or c " << endl;
    cout << " a= Find the largest number with a known quantity of whole two digit numbers " << endl;
    cout << " b= Find the smallest number with an unknown quantity of numbers " << endl;
    cout << " c= Quit " << endl;
    
    cin >> letter;
    
    if (letter == 'a')
    {
    	cout << "How many numbers would you like to enter? " << endl;
    	cin >> num;
    
    	cout << "Please enter a number and press enter. " << endl;
    	cout << endl;
    
    	ans = 0;
    
    	int count; // declaring loop control variable
    
    	for (count = 0; count < num; count = count++)
    	{
    		cin >> num1;
    		ans = num1;
    		cin >> num1;
    		if (num1 > ans)
    		{
    			ans = num1;
    			cin >> num1;
    		}
    
    		else if (ans > num1)
    		{
    			ans = ans;
    			cin >> num1;
    		}
    	}
    
    cout << "The Largest number you entered was " << ans << endl;
    cout << endl;
    return main();
    }
    
    else if (letter == 'b')
    {
    	cout << " Enter as many numbers as you like, when you are finished enter '99' " << endl;
    	cout << endl;
    
    	answ = 0;
    
    	while (answ != 99)
    	{
    		cin >> num1;
    		answ = num1;
    		cin >> num1;
    
    
    		if (num1 < answ)
    		{
    			answ = num1;
    			cin >> num1;
    		}
     
    		else if (answ < num1)
    		{
    			answ = ans;
    			cin >> num1;
    		}
    	}
    
    	cout << "The smallest number you entered was " << answ << endl;
    cout << endl;
    }
    
    
    system("pause");
    
    return 0;
    
    }
    Last edited by roweboats; 06-21-2010 at 02:23 PM.

  5. #5
    Registered User
    Join Date
    Jun 2010
    Posts
    2
    ok sorry about the indenting. this is my first term writing code. the first part if i enter to enter 3 numbers it takes 6 til the loop breaks and it tells me what the largest number is, and the second part it tells me answ is being used without being initialized and when i continue it doesnt break when 99 is entered

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    8
    you have a keyboard buffer issue with your first part. the keyboard buffer isn't emptying after you get the letter. there is still something there that you won't see. look up buffer help or ask for further if you dont find anything, though when I went through this I was able to find my answer.

    your second part may be with this...

    cin >> num1;
    answ = num1;
    cin >> num1;

    you are never letting 99 get through.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Properly indented:
    Code:
    #include <iostream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    
    int main()
    {
    
    	char letter; // users menu choice
    	int num; // the number of numbers that the user wants to enter
    	int num1; // user inputed variables
    	int ans; // largest variable
    	int answ; // smallest variable
    
    	cout << " Please type in your choice by entering a b or c " << endl;
    	cout << " a= Find the largest number with a known quantity of whole two digit numbers " << endl;
    	cout << " b= Find the smallest number with an unknown quantity of numbers " << endl;
    	cout << " c= Quit " << endl;
    
    	cin >> letter;
    
    	if (letter == 'a')
    	{
    		cout << "How many numbers would you like to enter? " << endl;
    		cin >> num;
    
    		cout << "Please enter a number and press enter. " << endl;
    		cout << endl;
    
    		ans = 0;
    
    		int count; // declaring loop control variable
    
    		for (count = 0; count < num; count = count++)
    		{
    			cin >> num1;
    			ans = num1;
    			cin >> num1;
    			if (num1 > ans)
    			{
    				ans = num1;
    				cin >> num1;
    			}
    
    			else if (ans > num1)
    			{
    				ans = ans;
    				cin >> num1;
    			}
    		}
    
    		cout << "The Largest number you entered was " << ans << endl;
    		cout << endl;
    		return main();
    	}
    
    	else if (letter == 'b')
    	{
    		cout << " Enter as many numbers as you like, when you are finished enter '99' " << endl;
    		cout << endl;
    
    		answ = 0;
    
    		while (answ != 99)
    		{
    			cin >> num1;
    			answ = num1;
    			cin >> num1;
    
    
    			if (num1 < answ)
    			{
    				answ = num1;
    				cin >> num1;
    			}
    
    			else if (answ < num1)
    			{
    				answ = ans;
    				cin >> num1;
    			}
    		}
    
    		cout << "The smallest number you entered was " << answ << endl;
    		cout << endl;
    	}
    
    
    	system("pause");
    
    	return 0;
    
    }
    Take a look at the red line. It assigns ans to answ. Where do you initialize ans? Where do you store data in it?
    Also, you shouldn't recursively call main. Instead, implement this logic using loops. It shouldn't be difficult so long as you write down the logic on paper.
    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

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM