-
September 23rd, 2018, 10:55 AM
#1
[RESOLVED] C++ While loop, Looking for a bit of help.
Hi guys,
So I've been give the task of writing a program so a user can input a module code and multiple coursework marks. When the user is finished entering the details they will type in "-1" and it will end the loop and display the average.
The problem i have is that when i go to calculate the average it keeps counting the "-1" as a course mark. I was wondering how to fix this as i have no idea.
Here is the code below.
Code:
#include <iostream>
//including packages
using namespace std;
int main() //declaring main
{
int modulecode = 0;
float average = 0;
int counter = 0; //declaring variables
float input = 0;//store user input
float total = 0; //store total
cout << "enter module code" << endl;
cin >> modulecode;
while (input !=-1)
{
cout << "Enter a coursework grade or -1 to quit : ";
cin >> input;
total = total + input;
counter = counter + 1;
}//end while
average = total / counter;
cout << "total is : " << total;
cout << "the average is : " << average;
return 0;
}
thanks in advance.
Last edited by 2kaud; September 23rd, 2018 at 11:25 AM.
Reason: Added code tags
-
September 23rd, 2018, 11:27 AM
#2
Re: C++ While loop, Looking for a bit of help.
-
September 23rd, 2018, 11:32 AM
#3
Re: C++ While loop, Looking for a bit of help.
[When posting code, please use code tags so that the code is readable. Go Advanced, select the formatted code and click '#']
Code:
cin >> input;
total = total + input;
You are obtaining the input value and adding it to the total without first checking that it's not -1. So -1 will be included in the average. You need to only add to total and increment the counter if the input is not -1.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2017 (15.9.7)
-
September 23rd, 2018, 11:33 AM
#4
Re: C++ While loop, Looking for a bit of help.
 Originally Posted by iiMortaaaL
Problem solved.
Great!
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2017 (15.9.7)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
On-Demand Webinars (sponsored)
|