My code when I run it gives a blank answer. It shouldn't be doing that, it should be asking for inputs repeatedly until I put in a non-integer. If someone could please give me tips as to where I went wrong that would be great. Thank you all.
//Declaration of variables
int Running_total = 0;
float Average = 0.0;
int User_input = 0;
int counter = 0;
int Highest_input = -1;
int Lowest_input = -1;
//1. Intro Program (optional)
#include <iostream>
usingnamespace std;
int main()
{
std::cout << "This program will give you the highest, lowest, and average of the integers you enter. To end enter anegative number \n";
//2. Ask user to enter a positive integer (or a negative integer to stop)
//3. Record the user's input
//4.#4. Test the input >= 0
while (User_input >=0)
cout << "Please enter an integer calue : ";
std::cin >> User_input;
{
//4a. If it is true that input >= 0
if (User_input >=0)
//4a1. Add the input to "running total"
Running_total = Running_total + User_input;
//4a2. Increment "number of inputs counter"
counter = counter + 1;
//4a3. Test if "number of inputs counter" is equal to 1
if (counter==1)
//4a31. If true, replace both "highest input" and "lowest input" with the input
Highest_input = User_input;
Lowest_input = User_input;
//4a5. If the input > "highest input" then replace "highest input" with input
if (User_input > Highest_input)
Highest_input = User_input;
//4a6. If the input < "lowest input" then replace "lowest input" with input
if (User_input < Lowest_input)
Lowest_input = User_input;
else;
cout << "The Highest value entered was: "<< Highest_input;
cout << "The Lowest value entered was: " << Lowest_input;
cout << "The Average of enteries was: " << Average;
//end
}
}
This is an infinite loop that doesn't do anything. Remove the semicolon at the end of the line if you want the statement that follows to be part of the loop.
Fixed and code is now in code tags. I seem to now have a repeating loop that never ends while just repeating my line 20 without taking the waiting for the input.
tcs, when u say that I need to block statements that will be the begin and end of a statement right? So, my while statement will be one block, my if's will be another block, then else will be the last block. I'm learning this as im program it so sorry if it was an obvious statement.
//Declaration of variables
int Running_total = 0;
float Average = 0.0;
int User_input = 0;
int counter = 0;
int Highest_input = -1;
int Lowest_input = -1;
//1. Intro Program (optional)
#include <iostream>
usingnamespace std;
int main()
{
std::cout << "This program will give you the highest, lowest, and average of the integers you enter. To end enter anegative number \n";
//2. Ask user to enter a positive integer (or a negative integer to stop)
//3. Record the user's input
//4.#4. Test the input >= 0
while (User_input >=0)
{
cout << "Please enter an integer calue : ";
std::cin >> User_input;
//4a. If it is true that input >= 0
if (User_input >=0)
//4a1. Add the input to "running total"
Running_total = Running_total + User_input;
//4a2. Increment "number of inputs counter"
counter = counter + 1;
//4a3. Test if "number of inputs counter" is equal to 1
if (counter==1)
//4a31. If true, replace both "highest input" and "lowest input" with the input
Highest_input = User_input;
Lowest_input = User_input;
//4a5. If the input > "highest input" then replace "highest input" with input
if (User_input > Highest_input)
Highest_input = User_input;
//4a6. If the input < "lowest input" then replace "lowest input" with input
if (User_input < Lowest_input)
Lowest_input = User_input;
else;
Average = Running_total / counter;
}
{
cout << "The Highest value entered was: "<< Highest_input << "\n";
cout << "The Lowest value entered was: " << Lowest_input << "\n";
cout << "The Average of enteries was: " << Average <<"\n";
;
}
//end
}
Now the only problem I'm having is that the program is allowing negative integers through. From what I put in I don't see why it is allowing it b/c it should only be allowed if it was great than or equal to 0.
//Declaration of variables
int Running_total = 0;
float Average = 0.0;
int User_input = 0;
int counter = 0;
int Highest_input = -1;
int Lowest_input = -1 ;
//1. Intro Program (optional)
#include <iostream>
usingnamespace std;
int main()
{
std::cout << "This program will give you the highest, lowest, and average of the integers you enter. To end enter anegative number \n";
//2. Ask user to enter a positive integer (or a negative integer to stop)
//3. Record the user's input
//4.#4. Test the input >= 0
while (User_input >=0)
{
cout << "Please enter an integer calue : ";
std::cin >> User_input;
{
//4a. If it is true that input >= 0
if (User_input >=0)
{
//4a1. Add the input to "running total"
Running_total = Running_total + User_input;
//4a2. Increment "number of inputs counter"
counter = counter +1;
//4a3. Test if "number of inputs counter" is equal to 1
if (counter==1)
//4a31. If true, replace both "highest input" and "lowest input" with the input
Highest_input = User_input;
Lowest_input = User_input;
//4a5. If the input > "highest input" then replace "highest input" with input
if (User_input > Highest_input)
Highest_input = User_input;
//4a6. If the input < "lowest input" then replace "lowest input" with input
if (User_input < Lowest_input)
Lowest_input = User_input;
}
else
{
Average = (Running_total / counter);
cout << "The Highest value entered was: "<< Highest_input << "\n";
cout << "The Lowest value entered was: " << Lowest_input << "\n";
cout << "The Average of enteries was: " << Average <<"\n";
}
}
}
}
//end
how can I set my lowest_input in my declaration of variables to a number that doesn't affect the inputs? also, my average isn't working the way it should. Could anyone give me tips to what im doing wrong?
tcs pointed you exactly where you needed to go and if you would have read that tutorial, the mistake your dealing with now would have become very obvious. It almost seems like what you are doing is waiting a bit, not putting any effort in or at least not as much as you should and then wait for someone else to fix your mistake.
I have read the review quite a few times. The only language iv ever worked in had been python and to my the spacing is set. However, apparently C++ has no use for spacing. Therefore, reading the guide where it only does single examples isn't helping my with my whole code.
Can someone at least tell me where my compound statement is missing. Even if it's not exact words, I would enjoy it if you could explain in words why the code doesn't work.
can I set my lowest_input in my declaration of variables to a number that doesn't affect the inputs?
One way to handle this is to first off initialize lowest_input at a VERY high value so that the first input becomes the lowest starting baseline. Follow a similar approach for highest by choosing a VERY low starting value, if you haven't already done it.
my average isn't working the way it should
The average value is being determined by integer/integer so 23/4 = 5, not 4.6. So, you need to modify the denominator to force a float answer.
The only language iv ever worked in had been python and to my the spacing is set. However, apparently C++ has no use for spacing.
That's right. You need more braces, c++ depends on them. It is also a good idea to use them, even when there is only 1 statement. Not strictly required, but good practise because it will save you one day when you add more code.
//Declaration of variables
int Running_total = 0; // don't have global variables, at least put them in main.
float Average = 0.0; //it's good you initialised them all
int User_input = 0;
int counter = 0;
int Highest_input = -1;
int Lowest_input = -1 ;
//1. Intro Program (optional)
#include <iostream>
usingnamespace std; // avoid doing this read about namespaces
int main()
{
double Running_total = 0.0; //avoid the integer division later
double Average = 0.0; //it's good you initialised them all
int User_input = 0;
int counter = 0;
int Highest_input = -1;
int Lowest_input = -1 ;
// split up really long std::cout statements
std::cout << "This program will give you the highest, lowest, and average of the integers you enter. \n" ;
std::cout << "To end enter a negative number \n";
//2. Ask user to enter a positive integer (or a negative integer to stop)
//3. Record the user's input
//4.#4. Test the input >= 0
while (User_input >=0)
{
std::cout << "Please enter an integer value : \n";
std::cin >> User_input;
//4a. If it is true that input >= 0
if (User_input >=0)
{
//4a1. Add the input to "running total"
Running_total = Running_total + User_input;
Running_total += User_input; // more concise
//4a2. Increment "number of inputs counter"
counter = counter +1;
++counter; // more concise
//4a3. Test if "number of inputs counter" is equal to 1
if (counter==1)
{ // brace for compound statement
//4a31. If true, replace both "highest input" and "lowest input" with the input
Highest_input = User_input; // indentation
Lowest_input = User_input;
}
//4a5. If the input > "highest input" then replace "highest input" with input
if (User_input > Highest_input)
{ // brace even though 1 statement
Highest_input = User_input;
}
//4a6. If the input < "lowest input" then replace "lowest input" with input
if (User_input < Lowest_input)
{
Lowest_input = User_input;
}
} // this brace lines up with the if on line 37 consistent indentation
else {
break; //ends the while loop early
}
} //end while
Average = (Running_total / counter);
std::cout << "The Highest value entered was: "<< Highest_input << "\n";
std::cout << "The Lowest value entered was: " << Lowest_input << "\n";
std::cout << "The Average of enteries was: " << Average <<"\n";
}}
}
} //end of main
Hopefully I did it all correctly, I just edited in place.