-
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;
}
-
What happens? Also, try indenting your code properly. It's difficult to read, otherwise.
-
What part is failing for you?
-
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;
}
-
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
-
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.
-
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.