Student in Intro Comp Sci Course

This is a discussion on Student in Intro Comp Sci Course within the C++ Programming forums, part of the General Programming Boards category; I am taking an introductory course in Comp Science. Last Chapter deals with C++ Programming. I have struggled through the ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    5

    Question Student in Intro Comp Sci Course

    I am taking an introductory course in Comp Science. Last Chapter deals with C++ Programming. I have struggled through the semester but happy to annouce a 76 average. However, C++ is going to be the death of me! Can someone explain why this program won't compile? I believe it has something to do with my int declarations.....I think!?
    Code:
    //Student Weighted Grade Average//
    #include <iostream>
    using namespace std;
    int main()
    {
    int L1, L2, L3, T1, T2, X, W1, W2, W3, A1, B1; 
    double avg;
    cout<<"Type Each Lab Score and then Press Enter" << endl;
    cin>>L1>>L2>>L3;
    avg = (L1+L2+L3)/(3.0)*.30;
    cout<<"Your Weighted Lab Average Is:" <<avg<<endl;
    cout<<"Type Each Test Score and then Press Enter"<< endl;
    cin>>T1>>T2;
    avg = (T1+T2)/ (2.0)*.40;
    cout<<"Your Weighted Test Average Is:" <<avg<<endl;
    cout<<"Type Your Final Exam Score and then Press Enter" << endl;
    cin>>X;
    A1 = (X*.30);
    cout <<"Type Your Weighted Averages and then Press Enter"<<endl;
    cin>>W1>>W2>>W3;
    B1 = (A1+avg+avg);
    if(B1 >=90)
    cout<<"Your Final Grade is: A" <<endl;
    else if (avg >=80)
    cout<<"Your Final Grade is: B" <<endl;
    else if (avg >=70)	
    cout<<"Your Final Grade is: C" <<endl;
    else if (avg >=60)
    cout <<"Your Final Grade is: D" <<endl;
    else if (avg <59)
    cout<<"You Have Not Passed This Course" << endl;
    system("PAUSE");
     return 0;
    }
    Please help!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Add:
    Code:
    #include <cstdlib>

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    compiles fine, won't do what you want it to do though
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    Sorry as i am truly a beginner, exactly where do I insert this?

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by rpmunlmtd View Post
    Sorry as i am truly a beginner, exactly where do I insert this?
    At the beginning. Next to your other include statement.

  6. #6
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    Doe it need to be separated by anything, and what exactly does <cstdlib> mean?

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    It will include the header which allows you to call the system() function (which you call at the end of your program).

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    Quote Originally Posted by bithub View Post
    It will include the header which allows you to call the system() function (which you call at the end of your program).
    Thanks, but it still will not compile, and I regret I do not understand . I am trying to add the weighted averages of 3 lab scores, 2 test scores, and 1 final. The labs are worth 30% of grade, tests 30% and final 40%. Does my code even come close? I'm receiving errors indicating extra tokens at end of #include directive as well as warnings regarding converting to int from double.

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Thanks, but it still will not compile
    What is your compile error? That code should compile fine.

  10. #10
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Quote Originally Posted by bithub View Post
    What is your compile error? That code should compile fine.
    Mine gives:
    Code:
    $ g++ -o cpptest test.cpp
    test.cpp: error: I/O error: file is poorly indented. Failed to read contents.
    ...ah, that -pedantic flag...

    To the OP:
    Indent. With spaces or tabs, something like:
    Code:
    int main()
    {
        cout << "Hello world." << endl;
        if(1 == 1) {
            cout << "World is sane." << endl;
        }
        return 0;
    }
    ...makes code x1000 times more readable.
    Last edited by Cactus_Hugger; 07-22-2009 at 10:05 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    >> I have struggled through the semester but happy to annouce a 76 average.

    Considering that you're have trouble adding a simple include directive, I'd say you're professor is being a little too generous.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  12. #12
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    404
    I won't give you the answer since the point if school is learning and applying what you learned, but ask yourself first if you are having an issue converting int to double. How do you fix that? You should think about declaring all of your variables int or double. Remember if you convert a double back to an int then you will lose the decimal places. So decide if you want the number to round (which will make the average innacurate) or if you should use all decimal numbers. In this code the warning actually shouldn't affect the outcome since you don't save the double back as an int. It compiles fine in my compiler just gives a warning. Secondly think about where you assinged value to avg.

    Code:
    cout<<"Type Each Lab Score and then Press Enter" << endl;
    cin>>L1>>L2>>L3;
    avg = (L1+L2+L3)/(3.0)*.30;
    cout<<"Your Weighted Lab Average Is:" <<avg<<endl;
    avg = "your lab average"
    Your next lines of code are

    Code:
    cout<<"Type Each Test Score and then Press Enter"<< endl;
    cin>>T1>>T2;
    avg = (T1+T2)/ (2.0)*.40;
    cout<<"Your Weighted Test Average Is:" <<avg<<endl;
    avg = 'your test average'
    You have just changed what the variable equals now without saving its previous value. So later in your code

    Code:
    B1 = (A1+avg+avg);
    in other words B1 = (A1+'your test average' +'your test average' )
    rather than B1 = (A1 + 'your test average' + 'your lab average')

    Lastly learn to make readable code. I hope there is a section on comments and code spacing in your text book. There was in my book back in highscool and that was like 7 yrs ago.

    Most of your math is correct. You are very close to having the program run successfully, just think about the data type you wish the variables to be (either int or double) and change the declarations. Then figure out how to get your test average and lab average saved seperately. If you didn't figure out how to do the #include right from earlier this might be a pointless post, but I hope this helped.

  13. #13
    Registered User
    Join Date
    Jul 2009
    Posts
    5
    Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Comp sci contests
    By Sfel in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 11-20-2007, 03:13 PM
  3. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 12:21 PM
  4. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM
  5. Comp Sci Student needs help. Please reply!
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-14-2002, 07:55 PM

Tags for this Thread


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