• 0

[C++] Adding variables as they're needed.


Question

I started writing a program that, so far, looks like the following:

int main()

{
	double grades;
	char answer[5];

	do {
 ?cout << "Please input the grades you want averaged:" << endl;
 ?cin >> grades;
 ?ifstatement(grades, answer);
	} while (answer[0] == 'y');

	cout << "Your grades were: " << grades << endl;
	return 0;
}

void ifstatement(double grades, char answer[5])

{
	if ?(grades >= 0 && grades <= 100)
 ?{cout << "Do you wish to input another grade? (y/n) "; cin >> answer;}
	else
 ?{cout << "Sorry, the grade must be between 0 and 100. Try again: "; cin >> grades;
 ?ifstatement(grades, answer);}
}

So far, it does its job. I just coded it this far to see if my functions work well together. What I want to do now is to allow the user to keep inputting values as they're needed.

For example, he inputs 5, and then the program asks if he wants to put it another one, and he says yes... and inputs 7.

Now the values he has are 5 and 7.

The program is to take the average of whatever values the user inputs. How can I make the program remember all the values the user inputs?

Link to comment
Share on other sites

15 answers to this question

Recommended Posts

  • 0

Well the way it is now, I don't know how it even works, in main you rely on an answer returned by ifstatement, but in there you return void.

after your cin, you should clear the buffer also so in case the user hits enter twice, etc.

anyways, im quickly editing to what I would do.

Link to comment
Share on other sites

  • 0

You don't even need to remember all the values the user inputs for this type of thing (assuming you aren't going to extend this program to work with the data at a later stage). All you need to do it keep a running total and counter for the number of values the user has entered. You can then work out the average from there.

Sorry if this doesn't apply, I only quickly read though your post.

Link to comment
Share on other sites

  • 0

Aight this is how i re-wrote it, assuming you can at least use an array:

#include <iostream>
using namespace std;

char ifstatement(double);

int main(void)
{
	unsigned int iter = 0;
	double temp = 0;
	double grades[100];
	char answer = 'y';

	while ('y' == answer)
	{
  cout << "Please input the grades you want averaged:" << endl;
  cin >> temp;
  cin.clear();

  answer = ifstatement(temp);

  if( 3 != answer )
  {
 	 grades[iter] = static_cast<double>(temp);
 	 iter++;
  }
  
  if( 'n' != answer)
 	 answer = 'y';
	}

	for( unsigned int ui(0); ui < iter; ui++ )
 	 cout << "Your grades were: " << grades[ui] << endl;

	return 0;
}

char ifstatement(double grades)
{
	char answer = 'n';

	if  (grades >= 0 && grades <= 100)
	{
  cout << "Do you wish to input another grade? (y/n) ";
  cin >> answer;
  cin.clear();

  if( 'Y' == answer )
 	 answer = 'y';
	}

	else
	{
  cout << "Sorry, the grade must be between 0 and 100. Try again: " << endl;
  answer = 3;
	}

	return answer;
}

Link to comment
Share on other sites

  • 0

Could you show me how to do that sticking as close as possible to this code?

using namespace std;

//declaration of functions
void ifstatement(double grades, char answer[5]);

//main function which is executed
int main()

{
	double grades;
	char answer[5];
	int number;

	cout << "How many grades do you want to average?: ";
	cin >> number;

	do {
  cout << "Please input the grades you want averaged: "<< endl;
  cin >> grades;
  ifstatement(grades, answer);
	} while (answer[0] == 'y');

	cout << "Your grades were: " << grades << endl;
	return 0;
}





void ifstatement(double grades, char answer[5])

{
	if  (grades >= 0 && grades <= 100)
  {cout << "Do you wish to input another grade? (y/n) "; cin >> answer;}
	else
  {cout << "Sorry, the grade must be between 0 and 100. Try again: "; cin >> grades;
  ifstatement(grades, answer);}
}

Link to comment
Share on other sites

  • 0

Does it have to be done using recursion? I don't think it's really necessary in this case, in fact it would make it a tiny bit harder since you have to keep sending the index where you are in the array, unless it's a global I suppose.

Link to comment
Share on other sites

  • 0

Just compare what I did with mine to yours; the only way you'll learn.

Yours is so messed up that I wouldnt know where to manually start because your problems overlap eachother, which is why i just re-designed with your code.

edit: I just noticed you added a prompt asking how many grades you want... the only way C++ will let you create a custom array of this size, is with the new keyword, which means its now DMA. (to my knowledge).

Link to comment
Share on other sites

  • 0
Just compare what I did with mine to yours; the only way you'll learn.

Yours is so messed up that I wouldnt know where to manually start because your problems overlap eachother, which is why i just re-designed with your code.

edit: I just noticed you added a prompt asking how many grades you want... the only way C++ will let you create a custom array of this size, is with the new keyword, which means its now DMA. (to my knowledge).

584757325[/snapback]

It's not quite DMA because you can't resize an array you made with new.

Link to comment
Share on other sites

  • 0
It's not quite DMA because you can't resize an array you made with new.

584757491[/snapback]

It is, but it isn't.

I guess the poster could also use a list or some other STL datatype.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.