Array issues!!

I am creating a program that will take up to 25 names from a user as well as 4 grades and return an individual average. Only problem is my average isn't being returned in a decimal form. It's probably a rookie mistake but I've been looking at this for hours and I could use another set of eyes. Please help!

#include <iomanip>
#include <string>
#include <fstream>
#include <iostream>
#include "stdafx.h"

using namespace std;

void main (void)

{//Variable Declaration
int a,b,c;
double sum;
string name[25];
double score[25][5];

fstream out("studentaverages.txt.", ios::out);
string str="Start";



a=0;
while(str!="N"){
cout<<"\n Type a student's name without any spaces.";//Name Input
cin>>name[a];
sum=0;
for(int b=0;b<4;b++)
{cout<<"\n Enter scores for this student.";//Score Input
cout<<"\nScore: "<<b+1<<":";
cin>>score[a][b];
sum=score[a][b]+sum;}
score[a][4]=(sum/4.0);

cout<< "Would you like to enter another student's records? Type N for no (case sensitive)";//Repetition Loop
cin>>str;
a++;
}
cout<<"\n Name Score 1 Score 2 Score 3 Score 4 Avg."<<endl;
out<<"\n Name Score 1 Score 2 Score 3 Score 4 Avg."<<endl;
for(c=0;c<a;c++){
cout<<setw(10)<<name[c];
out<<setw(10)<<name[c];
for(b=0;b<=4;b++)
cout<<setw(10)<<score[b][c];
out<<setw(10)<<score[b][c];
}
cout<<endl;
out<<endl;
}
First and foremost, make sure you use code tags for your next post, it's off to the left and looks like <>. Highlight your code and press that, or type [code]around your code[/code] = around your code. This retains your spacing and allows your code to be read much easier.

Secondly, your variables a,b,c are incredibly hard to follow and don't describe what they do. Typically, variables for counters in for loops are declared there and forgotten about, you don't need to declare them at the start of main.

With those two things being said, it's a simple mistake, possibly because you got confused with what each variable did, but an easy solution is to change these lines:
1
2
cout<<setw(10)<<score[b][c];
out<<setw(10)<<score[b][c];


To:
1
2
cout<<setw(10)<<score[c][b];
out<<setw(10)<<score[c][b];
Sorry, still new to this. However, you're a godsend. I spent way too long on this for such a simple mistake. Thanks!
It fixed my console view, but it's still having the repeating problem in the text file
What issue exactly? I'd suggest deleting the file and trying again. If you still have the same issue, post back. In the meantime, I'm going to look through your code and see if I notice anything.

Edit: If it displays fine on the console, and your cout/out statements are all identical, then the only solution would be to remove the file. Just double check to make sure that you changed BOTH lines.
Last edited on
Topic archived. No new replies allowed.