Thread View: alt.comp.lang.learn.c-c++
14 messages
14 total messages
Started by CM
Sun, 20 Oct 2002 20:55
Problematic Program
Author: CM
Date: Sun, 20 Oct 2002 20:55
Date: Sun, 20 Oct 2002 20:55
94 lines
3597 bytes
3597 bytes
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>
void main()
{
int noOfGrades=3;
int noOfStudents=5;
char names[noOfStudents]; // array of names (strings)
char ID[noOfStudents]; // array of ID's (strings)
double Grades[noOfStudents][noOfGrades]; // array of 5 arrays 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++)
{
points=0;
cout<<"Enter information for Student #"<<i+1<<":"<<endl;
cout<<"\tName: ";
cin.getline(names[i],20);
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;
}
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
Re: Problematic Program
Author: Josh Sebastian
Date: Sun, 20 Oct 2002 21:07
Date: Sun, 20 Oct 2002 21:07
51 lines
2669 bytes
2669 bytes
On Sun, 20 Oct 2002 16:55:02 -0400, "CM" <mckenzie992@comcast.net>
wrote:
>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>
>void main()
int main()
>{
>int noOfGrades=3;
>int noOfStudents=5;
>char names[noOfStudents]; // array of names (strings)
The above definition is illegal. Only a constant integral expression
can go inside the square brackets. Same goes for the following two
array definitions.
>char ID[noOfStudents]; // array of ID's (strings)
>double Grades[noOfStudents][noOfGrades]; // array of 5 arrays 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++)
I don't think that does what you want. ITYM
for(int i=0; i<noOfStudents; ++i)
You make similar errors later.
I don't know if that's everything... just skimmed the rest.
Josh
Re: Problematic Program
Author: Neil Butterworth
Date: Sun, 20 Oct 2002 21:08
Date: Sun, 20 Oct 2002 21:08
51 lines
2674 bytes
2674 bytes
"CM" <mckenzie992@comcast.net> wrote in message
news:ESudnc40b4i4hS6gXTWcow@comcast.com...
> Hello everyone,
> I thought I had finished a c++ V2
What does "V2" mean?
> program when I ended up having several
> unexpected compilation errors that I am not sure how to fix.
All compilation errors are unexpected.
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>
> void main()
> {
> int noOfGrades=3;
> int noOfStudents=5;
> char names[noOfStudents]; // array of names (strings)
> char ID[noOfStudents]; // array of ID's (strings)
> double Grades[noOfStudents][noOfGrades]; // array of 5 arrays each of 3
Your basic problem is here - the dimensions of automatic arrays in C++ must
be constants known at compile time. One way of fixing your code would be to
declare your dimension sizes as:
const int noOfGrades=3;
const int noOfStudents=5;
This will fix some, but not all. problems in your code. Patch these fixes
in, try to fix the remaining problems and if you can't post here again. But
remember, we don't do homework for you.
NeilB
Re: Problematic Program
Author: CM
Date: Sun, 20 Oct 2002 21:24
Date: Sun, 20 Oct 2002 21:24
155 lines
5271 bytes
5271 bytes
--
CM
"Neil Butterworth" <neil_butterworth@lineone.net> wrote in message
news:3db31a56$1_1@mk-nntp-1.news.uk.worldonline.com...
>
> "CM" <mckenzie992@comcast.net> wrote in message
> news:ESudnc40b4i4hS6gXTWcow@comcast.com...
> > Hello everyone,
> > I thought I had finished a c++ V2
>
> What does "V2" mean?
>
> > program when I ended up having several
> > unexpected compilation errors that I am not sure how to fix.
>
> All compilation errors are unexpected.
>
> 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>
> > void main()
> > {
> > int noOfGrades=3;
> > int noOfStudents=5;
> > char names[noOfStudents]; // array of names (strings)
> > char ID[noOfStudents]; // array of ID's (strings)
> > double Grades[noOfStudents][noOfGrades]; // array of 5 arrays each of 3
>
> Your basic problem is here - the dimensions of automatic arrays in C++
must
> be constants known at compile time. One way of fixing your code would be
to
> declare your dimension sizes as:
>
> const int noOfGrades=3;
> const int noOfStudents=5;
>
> This will fix some, but not all. problems in your code. Patch these fixes
> in, try to fix the remaining problems and if you can't post here again.
But
> remember, we don't do homework for you.
>
> NeilB
>
>
>
>
>
Neil,
Thanks for the help. I believe I have corrected all the errors however a
huge mess at run-time. again, not sure what direction to take, if you could
be so kind as to give your feedback (or anyone else for that matter)
Thanks again (code below)
#include <string>
#include <iostream>
using namespace std;
#include <stdlib.h>
void main()
{
const int noOfGrades=3;
const int noOfStudents=5;
char names[noOfStudents]; // array of names (strings)
char ID[noOfStudents]; // array of ID's (strings)
double Grades[noOfStudents][noOfGrades]; // array of 5 arrays each of 3
elements (each student has 3 double grades)
double GPA[5]; // array of gpa's (doubles)
float points;
int j;
int i;
for (i=0;i=noOfStudents-1;i++)
{
points=0;
cout<<"Enter information for Student #"<<i+1<<":"<<endl;
cout<<"\tName: ";
//cin.getline(names[20],[i]);
cin>>names[i];
cout<<"\n\tID: ";
//cin.getline(ID[i],5);
cin>>ID[i];
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;
}
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];
}
}
Re: Problematic Program
Author: Phobos
Date: Sun, 20 Oct 2002 21:27
Date: Sun, 20 Oct 2002 21:27
8 lines
1858 bytes
1858 bytes
"Neil Butterworth" <neil_butterworth@lineone.net> wrote in message news:3db31a56$1_1@mk-nntp-1.news.uk.worldonline.com... > All compilation errors are unexpected. LOL, not for me! P
Re: Problematic Program
Author: CM
Date: Sun, 20 Oct 2002 21:43
Date: Sun, 20 Oct 2002 21:43
143 lines
5038 bytes
5038 bytes
--
CM
"Neil Butterworth" <neil_butterworth@lineone.net> wrote in message
news:3db31a56$1_1@mk-nntp-1.news.uk.worldonline.com...
>
> "CM" <mckenzie992@comcast.net> wrote in message
> news:ESudnc40b4i4hS6gXTWcow@comcast.com...
> > Hello everyone,
> > I thought I had finished a c++ V2
>
> What does "V2" mean?
>
> > program when I ended up having several
> > unexpected compilation errors that I am not sure how to fix.
>
> All compilation errors are unexpected.
>
> 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>
> > void main()
> > {
> > int noOfGrades=3;
> > int noOfStudents=5;
> > char names[noOfStudents]; // array of names (strings)
> > char ID[noOfStudents]; // array of ID's (strings)
> > double Grades[noOfStudents][noOfGrades]; // array of 5 arrays each of 3
>
> Your basic problem is here - the dimensions of automatic arrays in C++
must
> be constants known at compile time. One way of fixing your code would be
to
> declare your dimension sizes as:
>
> const int noOfGrades=3;
> const int noOfStudents=5;
>
> This will fix some, but not all. problems in your code. Patch these fixes
> in, try to fix the remaining problems and if you can't post here again.
But
> remember, we don't do homework for you.
>
> NeilB
I didn't copy over the correct instance .. here is the runtime error.
#include <string>
#include <iostream>
using namespace std;
#include <stdlib.h>
void main()
{
const int noOfGrades=3;
const int noOfStudents=5;
char names[noOfStudents]; // array of names (strings)
char ID[noOfStudents]; // array of ID's (strings)
double Grades[noOfStudents][noOfGrades]; // array of 5 arrays each of 3
//elements (each student has 3 double grades)
double GPA[5]; // array of gpa's (doubles)
float points;
int i,j;
for (i=0;i<noOfStudents;i++)
{
j=0;
points=0;
cout<<"Enter information for Student #"<<i+1<<":"<<endl;
cout<<"\tName: ";
//cin.getline(names[20],[i]);
cin>>names[i];
cout<<"\n\tID: ";
//cin.getline(ID[i],5);
cin>>ID[i];
for (j=0;j<noOfGrades;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;
}
cout<<"\nName\t";
for (i=0;i<noOfStudents;i++)
{
cout<<"\t"<<names[i];
}
cout<<"\nID\t";
for (i=0;i<noOfStudents;i++)
{
cout<<"\t"<<ID[i];
}
for (j=0;j<noOfGrades;j++)
{
cout<<"\nGrade #"<<j+1<<"\t";
for (i=0;i<noOfStudents;i++)
{
cout<<"\t"<<Grades[i][j];
}
}
cout<<"\nGPA\t";
for (i=0;i<noOfStudents;i++)
{
cout<<"\t"<<GPA[i];
}
}
Re: [C++] Problematic Program
Author: Jonas Lindstr�m
Date: Sun, 20 Oct 2002 21:53
Date: Sun, 20 Oct 2002 21:53
168 lines
5263 bytes
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
Re: Problematic Program
Author: Victor Bazarov
Date: Sun, 20 Oct 2002 22:06
Date: Sun, 20 Oct 2002 22:06
24 lines
2099 bytes
2099 bytes
"CM" <mckenzie992@comcast.net> wrote...
> [...]
> Neil,
> Thanks for the help. I believe I have corrected all the errors however a
> huge mess at run-time. again, not sure what direction to take, if you
could
> be so kind as to give your feedback (or anyone else for that matter)
> [..]
> for (i=0;i=noOfStudents-1;i++)
I think you still have a problem here. If you want the cycle
to execute noOfStudents times, you need to write
for (i = 0; i < noOfStudents; ++i)
The execution of the 'for' loop happens _if_ the condition is
met, not _until_ it is met. Besides, in your variation, it is
the _assignment_ operator, not equality. Equality is designated
by _two_ symbols '=': i == noOfStudents-1. Although it is
a bit irrelevant...
Victor
--
Please remove capital A's from my address when replying by mail
Re: Problematic Program
Author: Victor Bazarov
Date: Sun, 20 Oct 2002 22:07
Date: Sun, 20 Oct 2002 22:07
22 lines
1530 bytes
1530 bytes
"CM" <mckenzie992@comcast.net> wrote...
> [...]
> I didn't copy over the correct instance .. here is the runtime error.
>
> #include <string>
> #include <iostream>
> using namespace std;
> #include <stdlib.h>
> void main()
Weren't you told to make it
int main()
> { [...]
Besides, _what_ runtime error?
Victor
--
Please remove capital A's from my address when replying by mail
Re: Problematic Program
Author: CM
Date: Sun, 20 Oct 2002 22:30
Date: Sun, 20 Oct 2002 22:30
75 lines
3349 bytes
3349 bytes
--
CM
"Victor Bazarov" <vAbazarov@dAnai.com> wrote in message
news:ur6a8ldf3qqc31@corp.supernews.com...
> "CM" <mckenzie992@comcast.net> wrote...
> > [...]
> > I didn't copy over the correct instance .. here is the runtime error.
> >
> > #include <string>
> > #include <iostream>
> > using namespace std;
> > #include <stdlib.h>
> > void main()
>
> Weren't you told to make it
>
> int main()
>
> > { [...]
>
>
> Besides, _what_ runtime error?
>
> Victor
> --
> Please remove capital A's from my address when replying by mail
>
>
Runtime error is that is allows one to type the first name of the student
but errors thereafter
a copy of below..
I changed to int main () however, there is no effect at the moment.
Enter information for Student #1:
Name: Craig
ID:
Grade #1:
Grade #2:
Grade #3: Enter information for Student #2:
Name:
ID:
Grade #1:
Grade #2:
Grade #3: Enter information for Student #3:
Name:
ID:
Grade #1:
Grade #2:
Grade #3: Enter information for Student #4:
Name:
ID:
Grade #1:
Grade #2:
Grade #3: Enter information for Student #5:
Name:
ID:
Grade #1:
Grade #2:
Grade #3:
Name C � � � �
ID r � � � �
Grade
-9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596
e+061 -9.25596e+061
Grade
-9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596
e+061 -9.25596e+061
Grade
-9.25596e+061 -9.25596e+061 -9.25596e+061 -9.25596
e+061 -9.25596e+061
GPA 4 4 4 4 4Press any key to continue
Re: Problematic Program
Author: Josh Sebastian
Date: Sun, 20 Oct 2002 23:41
Date: Sun, 20 Oct 2002 23:41
61 lines
3147 bytes
3147 bytes
On Sun, 20 Oct 2002 18:30:09 -0400, "CM" <mckenzie992@comcast.net>
wrote:
>Runtime error is that is allows one to type the first name of the student
>but errors thereafter
>a copy of below..
>const int noOfGrades=3;
>const int noOfStudents=5;
>char names[noOfStudents]; // array of names (strings)
This is not an array of strings. It is an array of individual
characters. For a given x, names[x] is *one* character. An array of
strings looks like this:
std::string names[noOfStudents];
Of course, that requires you to include the <string> header.
>char ID[noOfStudents]; // array of ID's (strings)
This is also an array of characters, not an array of strings.
>double Grades[noOfStudents][noOfGrades]; // array of 5 arrays each of 3
>//elements (each student has 3 double grades)
>double GPA[5]; // array of gpa's (doubles)
>float points;
>int i,j;
>
>for (i=0;i<noOfStudents;i++)
>{
>j=0;
>points=0;
>cout<<"Enter information for Student #"<<i+1<<":"<<endl;
>cout<<"\tName: ";
>//cin.getline(names[20],[i]);
>cin>>names[i];
This reads in the 'C' you typed. It only reads in one character
because names[i] is a char.
>cout<<"\n\tID: ";
>//cin.getline(ID[i],5);
>cin>>ID[i];
This tries to read the 'r' you typed into a single character, ID[i].
It succeeds.
>for (j=0;j<noOfGrades;j++)
>{
>cout<<"\t\nGrade #"<<j+1<<": ";
>cin>>Grades[i][j];
This tries to parse the "iag" part into a double. Of course, there's
no way to finagle a double out of "iag", so the read fails. By
default, when a read fails, cin won't let any subsequent read
operations succeed until you've acknowledged that there was an error
and clear the error state. You fail to ever check for an error, so all
subsequent read operations also fail.
HTH
Josh
Re: Problematic Program
Author: Jon Bell
Date: Sun, 20 Oct 2002 23:55
Date: Sun, 20 Oct 2002 23:55
27 lines
2395 bytes
2395 bytes
In article <HIacndmZkvBvgy6gXTWcoA@comcast.com>, CM <mckenzie992@comcast.net> wrote: > > [...] I believe I have corrected all the errors however a >huge mess at run-time. again, not sure what direction to take, if you could >be so kind as to give your feedback (or anyone else for that matter) Here's a piece of advice for your *next* program (probably too late to do any good on this one): *don't* try to write and debug the whole thing at once! As you've seen, this leads to tons of compiler errors, and then if you have logic problems it can be a major task to straighten them out. Instead, you should first sketch an outline of the overall structure of the program, then write and debug it in stages, a few lines at a time. As you get more experience, you'll be able to make each stage bigger. Add extra output statements to verify that variables contain what they're supposed to contain, loops are working the way they're supposed to work, etc. When you're done testing and debugging, remove those extra output statements. Oh, and get in the habit of indenting and formatting your code consistently as you write it. It will make it a lot easier for you to see problems. -- Jon Bell <jtbellap8@presby.edu> Presbyterian College Dept. of Physics and Computer Science Clinton, South Carolina USA
Re: Problematic Program
Author: Sleek
Date: Mon, 21 Oct 2002 03:32
Date: Mon, 21 Oct 2002 03:32
26 lines
1809 bytes
1809 bytes
"CM" <mckenzie992@comcast.net> wrote in message news:<ESudnc40b4i4hS6gXTWcow@comcast.com>...
> 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;
> }
You need 1 if statement (the first one) ... one else statement (the
last one) .. everything inbetween should be "else if"
Re: Problematic Program
Author: Thomas Matthews
Date: Mon, 21 Oct 2002 14:08
Date: Mon, 21 Oct 2002 14:08
76 lines
3292 bytes
3292 bytes
sleek@crapmail.com (Sleek) wrote in message news:<84654e56.0210201932.671b8a43@posting.google.com>...
> "CM" <mckenzie992@comcast.net> wrote in message news:<ESudnc40b4i4hS6gXTWcow@comcast.com>...
>
> > 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;
> > }
>
> You need 1 if statement (the first one) ... one else statement (the
> last one) .. everything inbetween should be "else if"
I believe the OP should eliminate the table and use an array:
const unsigned int LOW_RANGE_INDEX = 0;
const unsigned int HIGH_RANGE_INDEX = 1;
const unsigned int GRADE_POINT_INDEX = 2;
const unsigned int MAX_INDICES = 3;
const unsigned int MAX_RANGES = 7;
const unsigned int grade_scale[MAX_RANGES][MAX_INDICES] =
{
/* low high points * 10 */
/* ---- ---- ----------- */
{ 0, 59, 0},
{ 60, 64, 10},
{ 65, 69, 15},
{ 70, 74, 20},
{ 75, 79, 25},
{ 80, 84, 30},
{ 85, 89, 35},
{ 90, 100, 40}
};
/* ... */
/* Look up the grade */
for (unsigned int k = 0; i < MAX_RANGES; ++i)
{
if ((Grades[i][j] >= grade_scale[k][LOW_RANGE_INDEX])
&& (Grades[i][j] <= grade_scale[k][HIGH_RANGE_INDEX]))
{
points += (double)(grade_scale[k][GRADE_POINT_INDEX]) / 10.0;
break;
}
}
Or for faster service:
const unsigned int MAX_GRADE_VALUES = 100;
const double grade_values[MAX_GRADE_VALUES] =
{ 0.0, 0.0, /* ... */,
1.0, 1.0, 1.0, 1.0, 1.0, /* indices 60 ... 64 */
1.5, 1.5, 1.5, 1.5, 1.5, /* indices 65 ... 69 */
/* ... */
};
/* Look up the grade point */
if (Grades[i][j] <= MAX_GRADE_VALUES)
points += grade_values[Grades[i][j]];
-- Thomas
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads