Hello I am trying to write a program that prompts the user for 3 test scores, averages the 3 test scores, and keeps repeating until the user enters "N". My problem is that I am creating the average and exam scores as data type double but in order to check for "N" I am not sure what data type I should be using or if I should be converting. My code is the following:

void compute()
{
	char firstExam;
	double secondExam, thirdExam,avg;

	cout << "Enter the 1st test score (N to end): ";
	cin >> firstExam;

	while (firstExam != "N")
	{
		cout << "Enter the 2nd test score: ";
		cin >> secondExam;

		cout << "Enter the 3rd test score: ";
		cin >> thirdExam;


		avg = (firstExam + secondExam + thirdExam) / 3

		cout << avg << endl;

		cout << "Enter the 1st test score (N to end): ";
		cin >> firstExam;
	}
}

Recommended Answers

All 6 Replies

You could use a std::string for input then convert to double if its not "N".

void compute()
{
	char firstExam;
                std::string input;
	double secondExam, thirdExam,avg;

	cout << "Enter the 1st test score (N to end): ";
	cin >> input; //firstExam;

	while (input != "N")
	{
                      stringstream str(input);
                      str >> firstExam;
		cout << "Enter the 2nd test score: ";
		cin >> secondExam;

		cout << "Enter the 3rd test score: ";
		cin >> thirdExam;


		avg = (firstExam + secondExam + thirdExam) / 3

		cout << avg << endl;

		cout << "Enter the 1st test score (N to end): ";
		cin >> firstExam;
	}
}

You could use a std::string for input then convert to double if its not "N".

I am sorry but I am confused by your code. I tried re-arranging what I understood but it still does not work. I am trying to check if the input equals N. So I am trying to say while the input does not equal ā€œNā€ then make the firstExam (which is a double) equal the input and the prompt the user for the next 2 exams.

I am getting this error message, which creates other error messages:

error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

Also my other issue is that when it requests the user to enter the second exam, it does not allow them because it jumps to prompt them for the 3rd exam. Is there a way to stop this to allow them to enter the second exam and then prompt them for the 3rd exam?

So now my code is the following:

#include <iostream>
using std::cout; 
using std::cin;
using std::endl;
using std::string;

void compute()
{
	string input;
	double firstExam, secondExam, thirdExam,avg;

	cout << "Enter the 1st test score (N to end): ";
	cin >> input; 

	while(input != "N")
	{
		firstExam = input;

		cout << "Enter the 2nd test score: ";
		cin >> secondExam;

		cout << "Enter the 3rd test score: ";
		cin >> thirdExam;


		avg = (firstExam + secondExam + thirdExam) / 3

		cout << avg << endl;

		cout << "Enter the 1st test score (N to end): ";
		cin >> input;
	}
}
int main()
{
	compute();

	cout << endl;
	return 0;
}

AcientDragons's code seems to be working fine. You need to include the header files "string" and "sstream" to . You need to change a little bit of the code by just adding this

std::stringstream str(input);

your also missing a semi-colon in one of the lines of your code.
If you dont understand the way you can use the stringstream then you can read more in this link http://www.cplusplus.com/reference/iostream/stringstream/

AcientDragons's code seems to be working fine. You need to include the header files "string" and "sstream" to . You need to change a little bit of the code by just adding this

std::stringstream str(input);

your also missing a semi-colon in one of the lines of your code.
If you dont understand the way you can use the stringstream then you can read more in this link http://www.cplusplus.com/reference/iostream/stringstream/

Hello,

I made the corrections and tried to do this but it is still not working. Also I do not follow what we are trying to do with the string stream as I have not heard of this.

Here's my update code:

#include <iostream>
using std::cout; 
using std::cin;
using std::endl;

#include <string>
using std::string;

#include <sstream>
using std::stringstream;


void compute()
{
	string input;
	double firstExam, secondExam, thirdExam, avg;

	cout << "Enter the 1st test score (N to end): ";
	cin >> input; 

	while(input != "N")
	{
		firstExam = input;
		stringstream str(input);

		cout << "Enter the 2nd test score: ";
		cin >> secondExam;

		cout << "Enter the 3rd test score: ";
		cin >> thirdExam;


		avg = (firstExam + secondExam + thirdExam) / 3;

		cout << avg << endl;

		cout << "Enter the 1st test score (N to end): ";
		cin >> input;
	}
}
int main()
{
	compute();

	cout << endl;
	return 0;
}

stringstream (all one word) is a class that acts like fstream but instead of working with files it works with std::string.

line 24: delete that line. you are attempting to set a double to that of std::string, which is not possible. Re-read the code I posted vary carefully.

stringstream str(input);
str >> firstExam;

The above will convert the std::string into a double.

stringstream (all one word) is a class that acts like fstream but instead of working with files it works with std::string.

line 24: delete that line. you are attempting to set a double to that of std::string, which is not possible. Re-read the code I posted vary carefully.

stringstream str(input);
str >> firstExam;

The above will convert the std::string into a double.

I see :-D thank you! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.