2 Replies - 232 Views - Last Post: 07 January 2013 - 08:56 AM Rate Topic: -----

#1 fortemixx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-January 13

using only for and only do-while

Posted 07 January 2013 - 06:15 AM

need advice on how to re-write below simple code by using conditions 'for' only and 'do-while' only.
I tried using 'for' on the 1st portion, but the total not able to process accurately
So far, stuck in the 'for' condition. Yet to try do-while.

Need help to point out the mistakes, and further advice for using do-while

thank you!


for (int grade = 0; grade != -1; cout << "Enter grade or -1 to quit: ")
		gradeCounter++;
		cin >> grade;
		total += grade;



Original code to re-write:-
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int grade;
	int gradeCounter;
	int total;
	double average;
	
	gradeCounter = 0;
	total = 0;
	
	cout << "Enter grade or -1 to quit: ";
	cin >> grade;
	while (grade != -1)
	{
		total = total + grade;
		gradeCounter = gradeCounter + 1;
		
		cout << "Enter grade or -1 to quit: ";
		cin >> grade;
	}
	if (gradeCounter != 0)
	{
		average = static_cast < double >(total)/gradeCounter;
		cout << "\nTotal of all " << gradeCounter << " grades entered is " << total << endl;
		// out << "Class average is " << setprecision(3) << average << endl;
		cout << "Class average is " << setprecision(2) << fixed  << average << endl;
		cin.sync();
		cin.get();
	}
	else 
		cout << "No grades were entered" << endl;
	cin.sync();
	cin.get();
}




Is This A Good Question/Topic? 0
  • +

Replies To: using only for and only do-while

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4060
  • View blog
  • Posts: 12,540
  • Joined: 25-December 09

Re: using only for and only do-while

Posted 07 January 2013 - 08:24 AM

You may want to start by reviewing your information about how to use the different control structures available.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 fortemixx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 07-January 13

Re: using only for and only do-while

Posted 07 January 2013 - 08:56 AM

it works after reading your info in given link.

	for (int grade = 0; grade != -1; total += grade) 
		{
		gradeCounter++;
		cout << "Enter grade or -1 to quit: ";
		cin >> grade;
		}



thank you so much!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1