Complete Newbie to programming

the other day I wrote this code:

// This program calculates student exam average.

#include <iostream>
#include <string>

using namespace std;



int main()

{

double studScore;
double studAvg;
double Exam1;
double Exam2;
double Exam3;
double examTotal;

string studFirstName;
string studLastName;



// Get students first name.

cout << "Enter student's first name. ";

cin >> studFirstName;



// Get students last name.

cout << "Enter student's last name. ";

cin >> studLastName;



// Get exam 1 score.

cout << "grade from exam 1 = ";

cin >> Exam1;



// Get exam 2 score.

cout << "grade from exam 2 = ";

cin >> Exam2;



// Get exam 3 score.

cout << "grade from exam 3 = ";

cin >> Exam3;



// Calculate exam total.

studScore = Exam1 + Exam2 + Exam3;



// Calculate exam total.

studAvg = studScore/3;



// Display the pay.

cout << "average is " << studAvg << endl;

system ("pause");

return 0;

}


part 1
now I need to create a "if/else" statement that will
give a letter grade based on the average value ie:
if studAvg >=90 then grade is 'A'
if studAvg >=80 then grade is 'B'
if studAvg >=70 then grade is 'C'
if studAvg >=60 then grade is 'D'
else grade is 'F'

part 2
then after this i want to write a "user controlled loop" that
will ask me to enter the number of students in the classroom
and ask me to input the name of each student and then ask me for there 3 exam scores and then it average them and give me the letter grade

I am completely lost on this and I need some help. I know that the part that I wrote is the start, and I know that adding on the "if/else" should not be that hard, but I am completely lost on the "user control loop"

can someone please baby step me thru this.

closed account (j2NvC542)
Ok, part 1:
You basically already wrote the code. What you need is an if-statement, which looks like this:
if (condition) { some expressions; }. You want every following if-statement to be "else"-d, so if studAvg is greater or equal 90, the other "else"-d if-statements wouldn't be executed. Not necessary, but why executing all those if-statements if you know it's useless?

Part 2:
You want to declare a variable which stores the amount of students in the classroom. Then you need a for-loop. A for loop lets you declare a counter-variable, the condition that must be met and what should be done as the last expression. Usually it is incrementing the the counter-variable by one. It looks like this:
for (counter-variable declaration; condition; incrementing counter-variable) { This code gets executed if the condition evaluates to true; }.
As soon as the condition evaluates to false, you break out of the for-loop. So you want the for loop to go as long as the counter-variable is smaller than the amount of students. Inside the loop you will ask for students name and so on.

Actually I didn't tell you anything you couldn't have found by yourself, but well. At least I didn't write you the whole code, so you still might have something to look up :P
Oh, and by the way; next time please use code-tags on the right-hand side of the input box :)
????????? WHAT DOES THIS MEAN?

I am so sorry when I say I am new to c++ programming I mean as in what I have written is the only code i have ever written. So I know that what you put here make since to alot of people it is ancient chinese to me. Could you provide me with some samples that I could see. I am a visual learn.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Basic if/else
if( foo )
{
  // this code will run if 'foo' is true
}
else
{
  // this code will run if 'foo' is false
}

// else/if chain
if( foo )
{
  // this code will run if 'foo' is true
}
else if( bar )
{
  // this code will run if 'foo' is false AND 'bar' is true
}
else
{
  // this code will run if 'foo' is false AND 'bar' is false
}



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Basic for loop structure

for( initialize; condition; increment )
{
  // loop body
}

/*
'initialize' is performed once at loop start

'condition' is checked every time the loop is about to run.  If 'condition' is
true, the loop body runs.  Otherwise, the loop exits

'increment' is performed after each time the loop body runs
*/

// typical usage:

for(int i = 0; i < 3; ++i)
{
  cout << i << endl;
}

/*
Here, 'int i = 0' is performed first (initialization).

Then the condition is checked (i < 3).  Since that results in 'true', the loop body executes and '0' is printed

Once the loop body finishes, '++i' is executed, resulting in i=1

Condition is checked again (i<3).  Still true, so loop body run again.  '1' is printed.

++i increments i again.  Now i=2

Condition checked again.  Still true, loop runs again.  '2' is printed

++i increments i again.  Now i=3

Condition checked again.  This time the condition is false (i < 3 is false when i==3).  So the loop exits.
*/
Ok so i have successfully completed part one of adding the "if else" statement. This worked out alot easier than I thought it would.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// This program calculates student exam average.

#include <iostream>
#include <string>

using namespace std;



int main()

{

   int studScore;
   int studAvg; 
   int Exam1;
   int Exam2;
   int Exam3; 
   int examTotal;

   string studFirstName;
   string studLastName;

   char grade;


   // Get students first name.

   cout << "Enter student's first name. ";

   cin >> studFirstName;

   // Get students last name.

   cout << "Enter student's last name. ";

   cin >> studLastName;

   // Get exam 1 score.

   cout <<  "grade from exam 1 = ";

   cin >> Exam1;

   // Get exam 2 score.

   cout <<  "grade from exam 2 = ";

   cin >> Exam2;

   // Get exam 3 score.

   cout <<  "grade from exam 3 = ";

   cin >> Exam3;

   // Calculate exam total.

   studScore = Exam1 + Exam2 + Exam3;

   // Calculate exam total.

   studAvg = studScore/3;

   //if statement to display grade letter.

   if (studAvg >= 90) 
	   grade = 'A';
   else if (studAvg >= 80) 
	   grade = 'B';
   else if (studAvg >= 70) 
	   grade = 'C';
   else if (studAvg >= 60)
	   grade = 'D';
   else
	   grade = 'F';

   // Display the average.

   cout <<  "average is " 
	    << studAvg 
		<< " " 
		<<grade << endl;

   // Stop program

   system ("pause");

   return 0; 

}


Now it's on to part 2! Which is to add in a "user controlled loop" that
will
(1) ask me to enter the number of students in the classroom
(2) ask me to input the name of each student
(3) ask me to enter 3 exam scores for each student
(4) average them and give me the letter grade

So if you guys could help me get started on this part that would be wizard, and please provide visual examples they are much easier for me to wrap my mind around.

Thanks
So if you guys could help me get started on this part that would be wizard, and please provide visual examples they are much easier for me to wrap my mind around.


I can't really give a more clear example than I did in my previous post.

That same example:
1
2
3
4
for(int i = 0; i < 3; ++i)
{
  cout << i << endl;
}


output:
0
1
2


Notice how the loop runs 3 times. Combine that with the outline I gave above with how the loop logic actually works, and you should be able to figure out how to make this user controlled.
Last edited on
i am sorry i just don't get it i am not understanding the loop.

I have been asked to create this loop into my program:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
get the number of student

while (i < NumStudents)                        

     get student name

     get student grades

     cal gpa and letter grade

     diplay ...

      i = i + 1

end while


and I can not understand how. I know this is frustrating to you knowing how easy this is to do, but I am furious with the fact that i can not grasp this idea. i am looking all over the web for very str8 forward examples of this in a code similar to mine so i can understand how it is added to the code. If any of you can help me directly with my code i would be very thankful. If anyone can point me to some videos that example and show this in a way that a 5 year old could understand it that would help too. I have no idea of what i am doing and it is driving me crazy. Yes I want to learn this but it is tough when you have never seen it before and you only have a book saying "see how easy c++ is". NO I DONT!
Let me be clear I am a graphic designer/artist. I have been working in the field of graphic designs for over 15 years. I decide to go back to school to finish my degree and I was told I had to take a every easy c++ class. I am a artist, so if I can't see it in my mind it does not make since to me I hope that explaination helps you guys to help me.
Aren't you all forgetting that the guy needs to input the total points of the test as a constant , or a variable that can be changed?In order to get the average of the tests and see what the letter grade would be for the GPA based on those tests then there needs to be a "total points possible" variable (or constant, though it should be variable since this program could be used over and over again)...
closed account (j2NvC542)
I just wrote so much and accidentally closed my browser... Let's try again.

You can also use a while loop.
1
2
3
4
5
while (condition) {
    expression;
    expression;
    ...
}


The first a computer does when he 'sees' a while-statement, is evaluating the condition. If the condition is false, he will just ignore the whole while loop. If it is true, he will repeat the loop as many times as the condition is true. Or in other words; he will repeat it until the condition is false. The computer executes every expression inside the loop and then jumps back up to the head, to check what the condition evaluates to, and then acts accordingly. Either repeat it again or ignore and go on after the while loop. That is after the closing curly brace.

In your case you want to repeat the loop as often as there are students in the classroom. So you need a variable with the amount of students, which you will input yourself, and a counter variable that gets incremented everytime the while loops. With the counter variable, you know which student you are currently doing. Therefore your while-head looks like this: while (counter < NumStudents).

To input a value into a variable, you use cin >>.
To output a value of a variable, you use cout <<.

You basically already wrote the whole code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Get students first name.

   cout << "Enter student's first name. ";

   cin >> studFirstName;

   // Get students last name.

   cout << "Enter student's last name. ";

   cin >> studLastName;

   // Get exam 1 score.

   cout <<  "grade from exam 1 = ";

   cin >> Exam1;

   // Get exam 2 score.

   cout <<  "grade from exam 2 = ";

   cin >> Exam2;

   // Get exam 3 score.

   cout <<  "grade from exam 3 = ";

   cin >> Exam3;

   // Calculate exam total.

   studScore = Exam1 + Exam2 + Exam3;

   // Calculate exam total.

   studAvg = studScore/3;

This is what you want to execute multiple times. As many times as there are students in classroom. This is the code you want to have inside the while loop.

Try the tutorial on this site.
Look for "thenewboston" on youtube, I also used to learn with him :)
Topic archived. No new replies allowed.