Super Easy C++ program help

This is a discussion on Super Easy C++ program help within the C++ Programming forums, part of the General Programming Boards category; Hi all, I am a total newb when it comes to program, I am currently in the process of writing ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    Super Easy C++ program help

    Hi all, I am a total newb when it comes to program, I am currently in the process of writing my 3rd super simple program, and I am having a whole bunch of trouble..becuase I am trying to teach myself, and I am lost trying to do this with online tutorials and whatnot

    Alright this is my problem...I was given a file from a website....(studenttests.dat) It looked like this...
    It gives the students name and his 3 tests scores then his average..

    William Billy 79 82 83.5 81.0

    Jones Sally 85 90 95 90

    Smith Mark 80 80 80.5 80.1

    I have to be able to input the file into the program, Be able to calculate a certain students average by giving their name. and the program has to input the first 3 tests scores then average them...For some reason I cannot get the file into the program, I have tried from the A: drive and the C: drive, both to no avail...any help you experts out there could give a rookie like me would be GREAT...this is what I have so far..i know its very simple but like I said...Im a newb,
    Code:
    //This program will print out student grade reports.  
    
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    
    
    	int main ()
    
    {
    		
    	
    	string FirstName;
    	cout << "What is name of the students test you want?" << endl;
    	cin >> FirstName;
    	t2,t3,LastName;
    	string FirstName;	
    
    
    	ifstream inFile("C:/studenttests.dat");
    
    
    	
    	 >> FirstName >> LastName;
    	if (! inFile)
        {
            cout << "Error opening input file" << endl;
            return -1;
        }
    
    	while (inFile.get());
    		cout << inFile.get();
    
    
    	cout << fixed << showpoint;
    	cout << setprecision (2);
    	cout << "   S T U D E N T  G R A D E  R E P O R T S" ; 
    	cout << endl;
    	cout << endl;
    	cout << "STUDENT           TEST   TEST  TEST   FINAL" <<endl;
    	cout << " NAME              ONE    TWO  THREE   AVG" <<endl;
    	cout << endl;
    	
    
    	cout << endl << endl;
    	return 0;
    
    }

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    Code:
    ifstream inFile("C:\\studenttests.dat");
    It's a backslash with the filesystem (as opposed to internet based systems), and you need to escape it. By using one backslash, the OS would consider \s as an escape code.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    14

    Unhappy

    Oh man i posted the wrong program...after i was looking at it, I said to myself..thats not it... well here is the right one, i tried doing
    Code:
     ("C://studenttests.dat")
    but still nothing Sorry for that mistake Here is the real program and what the input file really is..

    Smith Bill 85 75 74
    Jones Ann 100 70 80
    Walker Thomas 30 90 92

    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    
    using namespace std;
    
    
    	int main ()
    {
    
    float lastn1,firstn1, t1,t2,t3, lastn2, firstn2, t21,t22,t23,lastn3, firstn3
    ,t31,t32,t33, avg1,avg2,avg3;
    
    ifstream inFile;
    inFile.open("C:\\studenttests.dat");
    inFile >> lastn1 >> firstn1 >> t1 >> t2 >> t3 >> lastn2 >> firstn2 >> t21 >> 
    		t22 >> t23 >> lastn3 >> firstn3 >> t31 >> t32 >> t33;
    
    if (! inFile)
      {
            cout << "Cant find file joe...." << endl;
            return 0;
    }
    
    avg1 = (t1+t2+t3)/3;
    avg2 = (t21+t22+t23)/3;
    avg3 = (t31+t32+t33)/3;
    
    cout << setprecision(2);
    cout << " S T U D E N T  G R A D E  R E P O R T"; 
    cout << endl << endl;
    cout << "STUDENT         TEST   TEST   TEST  FINAL";
    cout << endl;
    cout << " NAME           ONE    TWO   THREE  AVG" << endl;
    cout << endl;
    cout << setw(3) << avg1;
    cout << "lastn1", "firstn1";
    cout << avg1, avg2, avg3;
    	
    	return 0;
    }
    And this is what it SHOULD look like...
    Code:
     S T U D E N T  G R A D E  R E P O R T 
    STUDENT              TEST         TEST        TEST      FINAL
      NAME                  ONE        TWO        THREE     AVG
    
    BILL SMITH            85           75              74          78.0
    
    ANN JONES            100          70              80          83.3
    
    THOMAS WALKER   30            90             92          70.7
    
    AVERAGE                71.7         78.3          82.0       73.3
    Last edited by ftblstr2319; 03-01-2004 at 05:02 PM.

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by ftblstr2319
    Code:
    float lastn1,firstn1, t1,t2,t3, lastn2, firstn2, t21,t22,t23,lastn3, firstn3
    ,t31,t32,t33, avg1,avg2,avg3;
    First name and Last name are floats? What numeric value does Smith Bill represent?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    actually, they arent numeric values. They are supposed to be the names of the people, Like bill=firstn1....and so on, i was just unsure what word assignment to use.. (ch)??? or string , im not sure

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    float is only for numbers not characters

    You have to define lastn1,firstn1, lastn2, firstn2,lastn3, firstn3 as string or array of char so considering each name of maximum 9characters(letters) you have to do as shown
    Code:
    ch lastn1[10],firstn1[10], lastn2[10], firstn2[10],lastn3[10], firstn3 [10];
    anyway i suggest u use a class or struct
    try the following code
    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include<cstdlib>
    using namespace std;
    struct student
    {
    char lastn[10];
    char firstn[10];
    double mark1;
    double mark2;
    double mark3;
    };
    
    int main()
    {
    
    student m;
    double av[3]={0,0,0};
    ifstream inFile;
    inFile.open("C:\\studenttests.dat");
    if (inFile.fail())
    {
    cout<<"can not open studenttest file";
    exit(1);
    }
    for(int i=0;i<3;i++)
    {
    inFile>>m.lastn;
    inFile>>m.firstn;
    inFile>>m.mark1;
    inFile>>m.mark2;
    inFile>>m.mark3;
    av[i]=(m.mark1+m.mark2+m.mark3)/3;
    cout<<m.lastn<<setw(3)<<m.firstn<<setw(5)<<m.mark1<<" "<<m.mark2<<"  "<<m.mark3<<setw(5)<<av[i]<<endl;
    
    }
    inFile.close();
    return 0;
    
    }
    I hope it works

  7. #7
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Hey M, you really should format your code. Indentation should be used in all blocks.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    Originally posted by WaltP
    Hey M, you really should format your code. Indentation should be used in all blocks.
    sorry .. i didnt understand u .. can you explain what you said please?

  9. #9
    Registered User
    Join Date
    Mar 2004
    Posts
    14
    Alright guys thanks alot for your help,
    in my opinion even though im having a tough time,
    i think its kinda fun, and i keep coming back...
    must like problem solving i guess..

    Well anyways here is the problem I am running into...
    well from the code ghani gave me. I was able to
    come up with a bit of my own..Ill let you guys decide
    Code:
    #include <iostream>
    #include <string>
    #include <fstream>
    #include <iomanip>
    //#include <cstdlib>
    
    using namespace std;
    struct student
    {
    char lastn[10];          //not sure what this does but..
    char firstn[10];
    
    double  t1,t2,t3;
    };
    
    
    
    int main()
    
    {
    	student m;
    	double avg[3]={0,0,0};
    	double avg1[3]={0,0,0};
    ifstream inFile;
    inFile.open("C:\\studenttests.dat");
    
    if (inFile.fail())
    {
    	cout<<"can not open studenttests file";
    	exit(1);
    }
    cout << "   S T U D E N T  G R A D E  R E P O R T";
    cout << endl; 
    cout << "STUDENT         TEST   TEST   TEST  FINAL";
    cout << endl;
    cout << " NAME           ONE    TWO   THREE  AVG" << endl;
    cout << endl;
    for(int i=0;i<3;i++)
    {
    inFile >> m.lastn; 
    inFile >> m.firstn;
    inFile >> m.t1;
    inFile >> m.t2;
    inFile >> m.t3;
    
    avg[i] = (m.t1+m.t2+m.t3)/3;
    	cout << endl;
    	
    	cout<<m.firstn<<setw(1)<<" "<<m.lastn<<setw(8)<<m.t1<<setw(7)<<""<<m.t2<<setw(4)<<" "<<
    		m.t3<<setw(5)<<avg[i]<<endl;
    	cout << fixed << showpoint <<
    	setprecision(1);
    	
    }	
    	cout << endl;
    {
    	avg[i] = (m.t1 + m.t1 +m.t1) /3
    cout << "AVERAGE"<< " 	 "<<m.t1<<setw(1)<<endl;
    	cout << fixed << showpoint <<
    	setprecision(1);
    /*for(int g=0;g<1;g++)
    {
    	avg1[g] =(m.t1[g] /3);
    	
    
    }*/
    }
    	cout << endl;
    	inFile.close();
    	return 0;
    
    }
    
    Now the my proble is the 2 parts in bold...
    what I am trying to do is average all 
    3 of test ones scores together to
     get an average at the bottom, 
    which i have highlighted...I have tried 2 diff ways which i separated in /*...like scores 
    (Bills t1 + Ann's t1 + Thomas t1) /3 ..and get the average...
    
    Also im am trying to get all the colums aligned...l
    ike all under test1, test2 and test3 and average...
    I dont know how to explain what 
    i would like to do exactly...
    when i move the first t1, the other two move with it...
    And since there are 3 test1's , and a 
    different number of char. before each one, 
    say I setw(5), then it moves all 3 over 5...
    
     S T U D E N T  G R A D E  R E P O R T 
    STUDENT              TEST         TEST        TEST      FINAL
      NAME               ONE        TWO           THREE     AVG
    
    BILL SMITH            85           75              74          78.0
    
    ANN JONES            100          70              80          83.3
    
    THOMAS WALKER         30            90             92          70.7
    
    
    AVERAGE              71.7         78.3          82.0       73.3
    Last edited by ftblstr2319; 03-02-2004 at 09:38 AM.

  10. #10
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    you only have one structure

    Each time you pass through your loop to fetch the next person and thier grades you overwrite the previous persons information in the structure. You never have all three persons information at the same time. The only way to do that would be to create an array of structures or create a variable that keeps track of the sum through each iteration of the loop.

  11. #11
    Registered User
    Join Date
    Feb 2004
    Posts
    127

    Re: you only have one structure

    Originally posted by manofsteel972
    Each time you pass through your loop to fetch the next person and thier grades you overwrite the previous persons information in the structure. You never have all three persons information at the same time. The only way to do that would be to create an array of structures or create a variable that keeps track of the sum through each iteration of the loop.
    yes i think array is the best way for that but it's not necessary to make it array of structure .. i think it is possible to make three arrays of type double for each test

  12. #12
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,157
    Originally posted by M_Ghani
    sorry .. i didnt understand u .. can you explain what you said please?
    Make your code readable, use indentation (spaces):
    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include<cstdlib>
    using namespace std;
    struct student
    {
        char lastn[10];
        char firstn[10];
        double mark1;
        double mark2;
        double mark3;
    };
    
    int main()
    {
        student m;
        double av[3]={0,0,0};
        ifstream inFile;
    
        inFile.open("C:\\studenttests.dat");
        if (inFile.fail())
        {
            cout<<"can not open studenttest file";
            exit(1);
        }
        for(int i=0;i<3;i++)
        {
            inFile>>m.lastn;
            inFile>>m.firstn;
            inFile>>m.mark1;
            inFile>>m.mark2;
            inFile>>m.mark3;
            av[i]=(m.mark1+m.mark2+m.mark3)/3;
            cout<<m.lastn<<setw(3)<<m.firstn<<setw(5)<<m.mark1<<" "<<m.mark2
                <<"  "<<m.mark3<<setw(5)<<av[i]<<endl;
    
        }
        inFile.close();
        return 0;
    }
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  13. #13
    Registered User
    Join Date
    Feb 2004
    Posts
    127
    Originally posted by WaltP
    Make your code readable, use indentation (spaces):
    Okay ... I will use identation .. but really i wrote it without identation because i wrote the code on the forum .. i will try to write the code in VC++ compiler before posting it

    thanks alot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  2. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 06:49 PM
  3. Macro Program
    By daspope in forum Windows Programming
    Replies: 5
    Last Post: 04-25-2004, 04:02 AM
  4. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 09:49 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21