Yo everybody..need to know sumting

i was trying to create a program using a while loops (its also same as while statement rite?) but then i end up my program got crashed >.<".I juz wanna know how to create a program using a while loops and can give me a good example?

I really2 want to learn this C++ coz i'm very interested in it :)

Here is the code that the program that i try to do :

main()

{
int total,count,name,mark;
float average;

total = 0;

count = 1;

while(count<=10){
cout<<"Enter name :";
cin>>name;
cout<<"Enter mark :";
cin>>mark;
total = total + mark;

cout<<"total is :"<<total;
++count;
}

return 0;
}

when i compile it doesnt show any error but my program didnt work out the way i expected it to be.Seems like it calculate the total on its own and i cant enter the mark. Can any1 help me out?

really appreciate it guyz..thx
Last edited on
It works fine for me. A couple of suggestions though:
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
#include <iostream>
#include <string>
using namespace std;

int main()  // main _always_ returns int
{
  int total, count, mark;
  string name;  // In the US, at least, people cannot be named a number.

  total = count = 0;  // If 'count' starts at zero it will stop at ten. See [1].
  while (count < 10)
  {
    cout << "Enter student " << count+1 << "'s name: ";
    getline( cin, name );  // See [2]

    cout << "Enter student " << count+1 <<"'s grade: ";
    cin >> mark;
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );  // See [2]

    total = total +mark;
    ++count;
  }

  // See [3]
  cout << "The total is " << total << endl;
  cout << "The average is " << (double)total /count << endl;

  return 0;
}


[1] Remember, the averages is "total / count", but if you start counting at zero then (count >= 10) will only be true when count == 11. However, you only have 10 students. This is what you call a "fencepost" or "off by one" error. So I changed it to start counting at zero and stop when count becomes ten. The other way to fix it would have been just to subtract one from count after the loop terminates...

[2] User input is a tricky thing, but one fairly constant truth is that no matter what you ask the user to input he will have to press ENTER after every input. You should design your programs with this in mind and always get that '\n' (newline or ENTER) out of the input stream.

In the first case, the user was asked to input a string. Strings may have spaces in them, so I used getline(), which reads everything typed until the '\n', stores it in the string, then reads and throws away the '\n'.

In the second case, you used the >> operator to read a number from the stream. The >> operator only reads things it is asked to. In this case, a number. So the '\n' is still sitting there, and we have to get rid of it before the next time we ask the user for input. That's what the cin.ignore() statement does: ignore (that is, read and throw away) everything up to and including the next '\n' in the input.

[3] I moved the stuff that displayed both the total and the average out of the loop. I could have left it in the loop --it doesn't matter. Well, except that I only wanted to display it once, after all input was received, instead of ten times, once after _every_ input is received.


Your compiler may accept specifying main() without a return type, but that is non-standard. In C++, you _must_ specify "int main()" or "int main(int argc, char**argv)".

Also, for interest, in the USA you are allowed to name your poor kid just about any stupid thing you can think of. Except a number. Weird trivia for you.

Hope this helps.
thx Duoas. I really appreciate u point out some parts that i think i should really2 do some hard work remembering them :D

thx dude! i manage to work my program out.Really appreciate it dude ^^

Here is the code that i have mod. Thx Duoas ^^

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
#pragma hdrstop

//---------------------------------------------------------------------------

#pragma argsused
#include <iostream.h>
#include <string.h>

//The program begins
int main()
{

//Declaring the intergers.

  int total,count,mark;
  char name[50];
  float average;

  total=0;

  count=1;

//Using while statement to compute total marks.

  while(count<=10)
  {
	 cout<<"Enter student "<<count<<"'s name: ";
	 cin>>name;

	 cout<<"Enter student "<<count<<"'s mark: ";
	 cin>>mark;

	 total= total + mark;
	 ++count;
  }

//calculating the average of all 10 students marks.

  average= total / 10;

  cout<<"\n";
  cout<<"The average of students total marks is: "<<average;

  return 0;
}

//--------------------------------------------------------------------------- 


oh and btw..in US can name anything for our kid? that must be a 'lucky' kid :D

p/s: the total and count are actually 2 different things.Total is for the marks and count is for the students.


Last edited on
Glad it is working for you.

Some comments:

Unless you are using a really old version of Borland C++, your headers should be #include <iostream> (without the ".h").

When you calculate
average = total / 10;
you will always get an integer result, since both total and 10 are integers. To get a floating point result you need to do something like:
average = (float)total / 10;

Good job.
well i'm using borland 4.5 one..can any1 tell me where to get newest ver.?
Topic archived. No new replies allowed.