Im brand new to C++ & Im having trouble with this program im writing i got all the way to the end of it working im just having problems using the setw function in reading and outputting from a file. I have my code attatched for what mine looks like, input file, and another screen shot for what it should look like. If you can give me any pointers in where i went wrong id appreciate it. Thanks.
hgill006 0 Newbie Poster
Snow White 77 87 76 82 92 96 90
Jack Frost 67 77 87 88 86 89 96
Sleeping Beauty 90 89 78 88 86 85 93
Prince Charming 92 87 88 89 90 91 78
Rapunzel Repunzel 66 76 73 81 85 70 86
Santa Claus 90 89 95 86 88 94 99
Miss Horner 65 76 72 50 69 75 92
//IT 210 Business Applications with C++
//Programmer: Harry Gillespie
#include <iostream> //preprocessor directives
#include <iomanip>
#include <fstream>
#include <conio.h>
using namespace std;
int main () //start of main
{
cout<<"012345678901234567890123456789012345678901234567890123456789"<<endl;
cout<<"************************************************************"<<endl;
cout<<"*"<<setw(48)<<"IT 210 Business Applications with C++";
cout<<setw(11)<<"*"<<endl;
cout<<"*"<<setw(38)<<"Programmer: Harry Gillespie"<<setw(21)<<"*"<<endl;
cout<<"*"<<setw(30)<<"Date: March 4, 2010"<<setw(29)<<"*"<<endl;
cout<<"*"<<setw(59)<<"*"<<endl;
cout<<"*"<<setw(50)<<"Program Assignment 2: Student Grades II";
cout<<setw(9)<<"*"<<endl;
cout<<"*"<<setw(54)<<"This program reads student information from";
cout<<setw(5)<<"*"<<endl;
cout<<"*"<<setw(53)<<"an input text file and outputs the results";
cout<<setw(6)<<"*"<<endl;
cout<<"*"<<setw(51)<<"to the monitor as well as to a text file";
cout<<setw(8)<<"*"<<endl;
cout<<"************************************************************"<<endl;
cout<<" "<<endl;
cout<<"Welcome to the IT210 Grade Calculator!"<<endl;
cout<<" "<<endl;
cout<<"Examine the outputfile now before you proceed!"<<endl;
cout<<" "<<endl;
cout<<"Press any key to read and calculate the grades of all students";
cout<<" "<<endl;
getch (); //variable used in conio.h
cout<<" "<<endl;
cout<<"123456789012345678901234567890123456789012345678901234567890";
cout<<"1234567890"<<endl;
cout<<"============================================================";
cout<<"=========="<<endl;
cout<<"Student Name"<<setw(13)<<"Total"<<setw(12)<<"Program";
cout<<setw(7)<<"Test"<<setw(12)<<"Course"<<setw(9)<<"Grade"<<endl;
cout<<setw(26)<<"Points"<<setw(11)<<"Average"<<setw(10)<<"Average";
cout<<setw(10)<<"Average"<<endl;
cout<<"------------------------------------------------------------";
cout<<"----------"<<endl;
ifstream fin;
ofstream fout;
fin.open ("input.txt");
fout.open ("output.txt");
if (!fin){
cout<<"Input Failure";
system ("Pause");
return 1;
}
if (!fout){
cout<<"Output Failure";
system ("Pause");
return 1;
}
while (!fin.eof()){
char str[20],str2[20];
int s1,s2,s3,s4,s5,s6,s7,total,i;
char grade;
float pavg,tavg,cavg;
fin>>str>>str2;
fout<<str<<" "<<str2;
cout<<str<<" "<<str2;
fin>>s1>>s2>>s3>>s4>>s5>>s6>>s7;
total=s1+s2+s3+s4+s5+s6+s7;
fout<<setw(10)<<total;
cout<<setw(10)<<total;
pavg = (s1+s2+s3+s4+s5)/5;
fout<<setprecision(4)<<showpoint<<setw(10)<<pavg;
cout<<setprecision(4)<<showpoint<<setw(10)<<pavg;
tavg=(s6+s7)/2;
fout<<setprecision(4)<<showpoint<<setw(10)<<tavg;
cout<<setprecision(4)<<showpoint<<setw(10)<<tavg;
cavg=(pavg+tavg)/2;
fout<<setprecision(4)<<showpoint<<setw(10)<<cavg;
cout<<setprecision(4)<<showpoint<<setw(10)<<cavg;
if(cavg>90){ cout<<right<<setw(0)<<setw(10)<<"A"<<endl;}
else if(cavg>80){cout<<right<<setw(0)<<setw(10)<<"B"<<endl;}
else if(cavg>70){cout<<right<<setw(0)<<setw(10)<<"C"<<endl;}
else {cout<<setw(35)<<"F"<<endl;}
}
fin.close();
fout.close();
cout<<"============================================================";
cout<<"=========="<<endl;
system ("Pause");
return 0;
}
This attachment is potentially unsafe to open. It may be an executable that is capable of making changes to your file system, or it may require specific software to open. Use caution and only open this attachment if you are comfortable working with msword files.
Anarionist 1 Junior Poster in Training
youll get more responses if you post the actual code instead of the file people are to skeptical download anything in the first place and most people don't want to download source code that can easily fit on the webpage :P
lotrsimp12345 37 Posting Pro in Training
First of all instead of writing out all those numbers on top i would put it in a loop by using a variable increment that variable and so forth Also to create a book of * use \n and . Make your life a lot easier. Don't have to type as much :). Easy to reuse you code.
Instead of using all those char arrays use string and getline() which should allow you to put in a delimiter to stop at.
setw() sets the field width of the next item only. So I don't think you should be having problems. Maybe you could explain the problem.
#include <iostream>
using namespace std;
int main()
{
int j;
for(int i=0; i<=5; i++)
{
for(j=0; j<=9; j++)
{
cout<<j;
}
j=0;
}
return 0;
}
Do something like this with the remainder of the *. Make your code look less clutter and easier to read :).
hgill006 0 Newbie Poster
thanks for the for loop idea i implemented that, but basically for the getline i want to use that and the string just read like 20 spaces including blankspace after the word if thats possible. Because right now my input file has names of different sizes, so when i go to setw the next word they end up not equal in spaceing like:
Student name: Total Points: Program Avg:
Snow White 600 82.00
Sleeping Beauty 590 81.00
and i cant setw different on each line because it's in a loop.
lotrsimp12345 37 Posting Pro in Training
use getline till end of line using \n and then you can create substrings with delimeters.
maybe use getline() in conjunction with stream operators such as >> since you want to read in integers or numbers.
Edited by lotrsimp12345 because: n/a
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.