So I am new (as in week one of a class) to c++ and my assignment is to create a console application that prompts for the user to enter their name, then to enter 10 grades ranging from 0.0 to 100.0, then to display all the grades along with their name and point to the highest and lowest grades. My professor provided a skeleton which is
#include "stdafx.h"
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int _tmain(int argc, _TCHAR* argv[])
{
// define the variables and the arrays you need
cout << "Please enter your name (80 characters max):\n";
cin.getline(studentName, MAX, '\n');
cout << "Please enter ten grades to one decimal point:\n";
// loop to read in the grades and store them into an array
my questions are: how to create the loop and limit it to 10 entries
is float grades [10]an appropriate definition of the array and limiting the elements to 10
If there are resources or tutorials you would recommend I am all ears, learning this from a text with no face to face is challenging. Thanks and sorry for the noob question
#include <iostream>
#include <iomanip>
int main()
{
// define the variables and the arrays you need
constint MAX_NAME_LEN = 80 ;
constint MAX_NAME_SZ = MAX_NAME_LEN + 1 ; // +1 for the terminating null character
constint NGRADES = 10 ; // number of grades
constdouble MIN_GRADE = 0.0 ;
constdouble MAX_GRADE = 100.0 ;
char name[MAX_NAME_SZ] = "" ; // array to hold the name.
double grades[NGRADES] ; // array to hold the grades.
for( int i = 0 ; i < NGRADES ; ++i ) grades[i] = MIN_GRADE ; // to start with, set all the grades to MIN_GRADE
std::cout << "Please enter your name (" << MAX_NAME_LEN << " characters max):\n";
std::cin.getline( name, MAX_NAME_SZ );
if( std::cin.fail() ) // if more than MAX_NAME_LEN characters were entered
{
// http://en.cppreference.com/w/cpp/io/basic_ios/clear
std::cin.clear() ; // clear the error flag
// http://en.cppreference.com/w/cpp/io/basic_istream/ignore
std::cin.ignore( 10000, '\n' ) ; // throw away the remaining characters in that line
}
std::cout << "Please enter " << NGRADES << " grades to one decimal point:\n";
// loop to read in the grades and store them into an array
// determine the highest and the lowest grade
double lowest_grade = MAX_GRADE ;
double highest_grade = MIN_GRADE ;
for( int i = 0 ; i < NGRADES ; ) // NGRADES times
{
std::cout << "grade #" << i+1 << ": " ;
std::cin >> grades[i] ; // note: if input fails, we leave the remaining grades as MIN_GRADE
if( grades[i] >= MIN_GRADE && grades[i] <= MAX_GRADE ) // if the grade is valid
{
if( grades[i] < lowest_grade ) lowest_grade = grades[i] ;
if( grades[i] > highest_grade ) highest_grade = grades[i] ;
++i ; // increment i to move to the next grade
}
else std::cout << "grade must be between " << MIN_GRADE << " and " << MAX_GRADE << '\n' ;
}
std::cout << "\nStudent " << name << "\n"
// http://en.cppreference.com/w/cpp/io/manip/setprecision
<< std::fixed << std::setprecision(1) // print exactly one digit after the decimal point
<< "Highest Grade: " << highest_grade << "\n"
<< "Lowest Grade: " << lowest_grade << "\n";
}