First time C++ user

Nov 9, 2015 at 6:43pm

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//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>
using namespace 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    
}
}
Last edited on Nov 9, 2015 at 7:03pm
Nov 9, 2015 at 6:58pm
 
while (User_input >=0);

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.
Nov 9, 2015 at 7:00pm
Please indent your code and put it into code-tags (f.e. by using those little nice "Format" buttons at the right of the editor window.
Nov 9, 2015 at 7:01pm
When you post code, please enclose with code tags.

Line 19 is your problem and then when you fix that, you'll need to figure out why it doesn't do what you want.
Nov 9, 2015 at 7:01pm
After doing as you said Peter87, I now have a loop that just keeps asking the user for an integer value. What am I doing wrong at this part?
Nov 9, 2015 at 7:07pm
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.
Nov 9, 2015 at 7:08pm
Now your while loops body does consist only of the cout-statement. Use a block statement instead.
1
2
3
4
while (/* any condition */)
{
    // Some statements
}




EDIT: "cout" instead of "cost".
Last edited on Nov 9, 2015 at 7:09pm
Nov 9, 2015 at 7:27pm
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.
Nov 9, 2015 at 7:36pm
You'll find some interesting information at
http://www.cplusplus.com/doc/tutorial/control/
Nov 9, 2015 at 7:38pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//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>
using namespace 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.
Nov 9, 2015 at 7:42pm
Again: Have a look at
http://www.cplusplus.com/doc/tutorial/control/
. There "block statements" are called "compound statements".
Nov 10, 2015 at 1:07am
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//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>
using namespace 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?
Nov 10, 2015 at 1:21am
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.
Nov 10, 2015 at 1:25am
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.
Nov 10, 2015 at 2:06am
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.
Nov 10, 2015 at 2:20am
closed account (48T7M4Gy)
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.
Last edited on Nov 10, 2015 at 2:59am
Nov 10, 2015 at 2:47am
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//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>
using namespace 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.


Last edited on Nov 10, 2015 at 2:52am
Nov 10, 2015 at 2:53am
With the indentation and bracing, you should be able to get your IDE to do it for you automatically.
Topic archived. No new replies allowed.