🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #100

Re: [C++] Problematic Program

#100
From: Jonas Lindstr�m
Date: Sun, 20 Oct 2002 21:53
168 lines
5263 bytes
"CM" <mckenzie992@comcast.net> wrote in
alt.comp.lang.learn.c-c++: 

> Hello everyone,
> I thought I had finished a c++ V2 program when I ended up
> having several unexpected compilation errors that I am not
> sure how to fix.  The jist of the program is to take 5
> student names, 5 ids for those students, 3 grades for each
> student and then calculate gpa displaying in a table format: 
> Name    Name 1     Name 2
> Id            999            222
> Grade 1     90        80
> Grade 2     90        80
> Grade 3
> GPA
> etc.
> 
> I have included what I have so far, was hoping someone could
> help to find my mistake.
> #include <string>
> #include <iostream>
> using namespace std;
> #include <stdlib.h>

You should include <cstdlib> instead. stdlib.h is sort of 
deprecated.

> void main()

main returns int.

> {
> int noOfGrades=3;
> int noOfStudents=5;
> char names[noOfStudents]; // array of names (strings)
> char ID[noOfStudents]; // array of ID's (strings)

Firstly, you have not declared arrays of strings. Replace char 
with string. Why else would you include the string header?

Secondly, the array sizes are not constant. Your compiler must 
have told you something in that line. Make noOfStudents const, or 
use dynamic allocation (new).

> double Grades[noOfStudents][noOfGrades]; // array of 5 arrays

noOfGrades must also be const.

> each of 3 elements (each student has 3 double grades)
> double GPA[5]; // array of gpa's (doubles)
> double points;
> 
> for (i=0;i=noOfStudents-1;i++)

You haven't declared i. Write int i = 0 inside the loop statement.

You also have two misunderstandings: how for-loops work, and the 
difference between assignment and comparison.

The second expression in a for statement is evaluated before each 
iteration just like the condition in a while statement: if the 
expression is true (non-zero), run the statement(s) controlled by 
the for statement. The expression often contains a relational 
operator.

  for (int i = 0; i < n; ++i) {
    // statements
  }

The above idiom (perform a task n times) is so common that you 
should be able to write it without thinking.

Replace n with noOfStudents in your for-loop, and it will loop as 
many times as there are students.

What you perhaps don't realize is that what you have now is an 
infinite loop. This is because you have used = when you meant ==.
Each time the expression

  i=noOfStudents-1

is evaluated, i will get the value noOfStudents-1, and the value 
of the expression will be checked against zero. Since the result 
will always be 4, and thus true (non-zero), the loop will run 
forever.

> {
> points=0;
> cout<<"Enter information for Student #"<<i+1<<":"<<endl;
> cout<<"\tName: ";
> cin.getline(names[i],20);

If you change names to be an array of strings, use

  getline(cin, names[i]);


> cout<<"\n\tID: ";
> cin.getline(ID[i],5);
> for (j=0;j=noOfGrades-1;j++)
> {
> cout<<"\t\nGrade #"<<j+1<<": ";
> cin>>Grades[i][j];
> if (Grades[i][j] >= 0  && Grades[i][j] <= 59)
> points+=0;
> if (Grades[i][j] >= 60 && Grades[i][j] <= 64)
> points+=1.0;
> if (Grades[i][j] >= 65 && Grades[i][j] <= 69)
> points+=1.5;
> if (Grades[i][j] >= 70 && Grades[i][j] <= 74)
> points+=2.0;
> if (Grades[i][j] >= 75 && Grades[i][j] <= 79)
> points+=2.5;
> if (Grades[i][j] >= 80 && Grades[i][j] <= 84)
> points+=3.0;
> if (Grades[i][j] >= 85 && Grades[i][j] <= 89)
> points+=3.5;
> else
> points+=4.0;
> }
> GPA[i]=points/noOfGrades;
> j=0;

Don't mess with j. Just use the loop idiom described earlier.

> 
> }
> 
> cout<<"\nName\t";
> for (i=0;i=noOfStudents-1;i++)
> {
> cout<<"\t"<<names[i];
> }
> 
> cout<<"\nID\t";
> for (i=0;i=noOfStudents-1;i++)
> {
> cout<<"\t"<<ID[i];
> }
> 
> for (j=0;j=noOfGrades-1;j++)
> {
> cout<<"\nGrade #"<<j+1<<"\t";
> for (i=0;i=noOfStudents-1;i++)
> {
> cout<<"\t"<<Grades[i][j];
> }
> }
> 
> cout<<"\nGPA\t";
> for (i=0;i=noOfStudents-1;i++)
> {
> cout<<"\t"<<GPA[i];
> }
>
> 
> }
> --
> CM
> 
> 
> 

HTH.

-- 
Jonas Lindstr�m <jonas.li@chello.se>
Stockholm, Sweden
Technical Details
Message-ID: <Xns92ADF384D47D6jonaslichellose@193.150.195.50>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed1.bredband.com!bredband!news01.chello.se!not-for-mail
References: <ESudnc40b4i4hS6gXTWcow@comcast.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 10d4ba,635d58b7486c58ea
X-Google-Attributes: gid10d4ba,public
X-Google-Thread: 109fba,635d58b7486c58ea
X-Google-Attributes: gid109fba,public
X-Google-Thread: 114592,635d58b7486c58ea
X-Google-Attributes: gid114592,public
X-Google-ArrivalTime: 2002-10-20 14:52:30 PST
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!newsfeed1.bredband.com!bredband!news01.chello.se!not-for-mail
Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++
Subject: Re: [C++] Problematic Program
From: "Jonas Lindstr�m" <jonas.li@chello.se>
References: <ESudnc40b4i4hS6gXTWcow@comcast.com>
Organization: Your Company
Message-ID: <Xns92ADF384D47D6jonaslichellose@193.150.195.50>
User-Agent: Xnews/5.03.24
Lines: 168
Date: Sun, 20 Oct 2002 21:53:23 GMT
NNTP-Posting-Host: 213.89.145.43
X-Complaints-To: abuse@chello.se
X-Trace: news01.chello.se 1035150803 213.89.145.43 (Sun, 20 Oct 2002 23:53:23 MET DST)
NNTP-Posting-Date: Sun, 20 Oct 2002 23:53:23 MET DST