why won't this work

This is a discussion on why won't this work within the C++ Programming forums, part of the General Programming Boards category; Code: # include <iostream> # include <cmath> using namespace std; int average, sd, s1, s2, s3, s4, deviation; int main() ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    why won't this work

    Code:
    # include <iostream>
    # include <cmath>
    using namespace std;
    
    int average, sd, s1, s2, s3, s4, deviation;
    
    int main()
    {
    	cout << "Press enter after entering a value.\n";
    	cout << endl;
    	cout << "Enter score one: ";
    	cin >> s1;
    	cout << "\nEnter score two: ";
    	cin >> s2;
    	cout << "\nEnter score three: ";
    	cin >> s3;
    	cout << "\nEnter score four: ";
    	cin >> s4;
    	cout << "\nThe average of your four scores is "<<average<<".\n";
    	cout << "The stand deviation of your scores is "<<sd<<".\n";
    
    	average =(s1+s2+s3+s4)/4;
    	deviation =(((s1-average)*
    		(s1-average))+((s2-average)*
    		(s2-average))+((s3-average)*
    		(s3-average))+((s4-average)*
    		(s4-average)))/3;
    	sd = sqrt(deviation);
    
    	return 0;
    }
    &#91;code]&#91;/code]tagged by Salem

  2. #2
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    Here.... look at the difference, and think about it, I think you'll figure it out
    Code:
    # include <iostream>
    # include <cmath>
    using namespace std;
    
    int average, sd, s1, s2, s3, s4, deviation;
    
    int main()
    {
        cout << "Press enter after entering a value.\n";
        cout << endl;
        cout << "Enter score one: ";
        cin >> s1;
        cout << "\nEnter score two: ";
        cin >> s2;
        cout << "\nEnter score three: ";
        cin >> s3;
        cout << "\nEnter score four: ";
        cin >> s4;
    
        average =(s1+s2+s3+s4)/4;
        deviation =(((s1-average)*(s1-average))+((s2-average)*(s2-average))+((s3-average)*(s3-average))+((s4-average)*(s4-average)))/3;
        sd = sqrt(deviation);
    
        cout << "\nThe average of your four scores is "<<average<<".\n";
        cout << "The stand deviation of your scores is "<<sd<<".\n";
    
        return 0;
    }

  3. #3
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You are outputting the values before you are computing them for one. Second, you probably want to use doubles for the average and standard deviation (if not all the inputs as well) so they can have decimals.
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  4. #4
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    lol, look at his code again Inquirer.... see the error?

    Oh, and yea... good idea SS

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    You can use cin to get pretty much every basic type (perhaps not bools).
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    BMJ:
    so can you input integers thru cin? *is confused* i never thought you could.........



    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  7. #7
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    sure.. you can do this:
    Code:
    int x;
    std::cin >> x;
    And if they enter an integer, it will be stored as x... if they do not enter an integer.... heh, well you'll have a problem.

  8. #8
    Rebooted Inquirer's Avatar
    Join Date
    Apr 2002
    Posts
    281
    hehe. I didn't even look that far. But, i learned something today. thanks.

    ~Inquirer
    Compilers:
    GCC on Red Hat 8.1 (Primary)
    GCC on Mac OS X 10.2.4 (Secondary)

    Others:
    MinGW on XP

  9. #9
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    You should use atoi() when getting integer input with cin

  10. #10
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    Thanks you guys

    Thank you very much, I understand what i did wrong

  11. #11
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    I knew you could figure it out

  12. #12
    Registered User
    Join Date
    Oct 2002
    Posts
    14

    new problem

    How do i get this code to loop infinitely until the user tells it to stop?

    # include <iostream>
    # include <cmath>
    using namespace std;

    double average, sd, s1, s2, s3, s4, deviation;

    int main()
    {
    cout << "Press enter after entering a value.\n";
    cout << endl;
    cout << "Enter score one: ";
    cin >> s1;
    cout << "\nEnter score two: ";
    cin >> s2;
    cout << "\nEnter score three: ";
    cin >> s3;
    cout << "\nEnter score four: ";
    cin >> s4;

    average =(s1+s2+s3+s4)/4;
    deviation =(((s1-average)*(s1-average))+((s2-average)*(s2-average))+((s3-average)*(s3-average))+((s4-average)*(s4-average)))/3;
    sd = sqrt(deviation);

    cout << "\nThe average of your four scores is "<<average<<".\n";
    cout << "The standard deviation of your scores is "<<sd<<".\n";

    return 0;
    }

  13. #13
    BMJ
    BMJ is offline
    Banal internet user BMJ's Avatar
    Join Date
    Aug 2002
    Location
    Chicagoland
    Posts
    1,380
    What do you mean by loop? Do you want it to go through getting all the scores? Or do you want the use to be able to stop at any time by typing 'Q' or something?

  14. #14
    Registered User
    Join Date
    Oct 2002
    Posts
    14
    what i mean is that i would like the user to be able to repeat the program. i would like the user to be able to input different scores each time. This would happen over and over again until the user wanted to stop. For instance,

    average
    mean

    "would you like to run the program again? y or n"

    then they would hit "y" as long as they wanted to use the program. when they want to quit, they would hit "n"

  15. #15
    Just a Member ammar's Avatar
    Join Date
    Jun 2002
    Posts
    953
    Hi,
    I would really like to know how did you input integers without using cin?
    none...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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