problem with numerical calculations

This is a discussion on problem with numerical calculations within the C++ Programming forums, part of the General Programming Boards category; Hi there, I'm trying to write a program which calculates the average mark of three tests but I can't figure ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72

    problem with numerical calculations

    Hi there,

    I'm trying to write a program which calculates the average mark of three tests but I can't figure out how to do the basic math, I know this sounds stupid but I can't seem to find any examples anywhere.

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        int pass = 15;
        int french, german, spanish;
        int mark = (french+german+spanish)/3;
        
        cout<<"LANGUAGE COURSE\n";
        cout<<"\n";
        cout<<"Enter French mark.";     // I don't need the cursor to move             
        cin>> french;                             // down.
        cin.ignore();
        cout<<"Enter German mark."; 
        cin>> german;
        cin.ignore();
        cout<<"Enter Spanish mark.";
        cin>> spanish;
        cin.ignore();
        cout<<"Your percentage mark was "<< mark <<".\n";
        cin.get();
    }
    It compiles and runs but when I enter for example 21 for french, 18 for german and 13 for spanish it displays the average 697034459.
    i have never done maths in c++ before and I can't find any relevant help anywhere so any links or other help will be very welcome.

    Calef13

  2. #2
    Registered User
    Join Date
    Jan 2006
    Posts
    20
    You have to assign french, german and spanish a number first, because else it will go wrong.

    do it like this:

    int french = 0, german = 0, spanish = 0;

    This way the 0's will be overwritten by the user's input, and displays the average.

  3. #3
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    And you shoud do the calculation of the average after the input of the marks.
    Kurt

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    20
    That's correct, because (0 + 0 + 0)/3 = 0.
    My bad.

  5. #5
    Registered User
    Join Date
    Dec 2005
    Location
    german border
    Posts
    72
    Thanks a lot , it's working brilliant, not that I doubted it would

    Calef13

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Input From a Text File - A Specific Problem.
    By Eddie K in forum C Programming
    Replies: 4
    Last Post: 03-09-2006, 02:50 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. array problem?
    By ssjnamek in forum C Programming
    Replies: 14
    Last Post: 02-08-2006, 05:03 PM

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