Thread: C++ Help

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    6

    C++ Help

    After building and debugging in Microsoft Visual Studio 2010. The sum, average and product is not calculating right. Also the large and small is not showing up in results.
    When I input: 13, 27, 14


    it gives me this:
    http://i54.tinypic.com/vy0hsp.jpg

    The Code I currently have:

    Code:
    #include <iostream>
    
    // supports std
    using namespace std;
    
    // executes program
    int main ()
    {
    	
    	int x;
            int y;
            int z;
    	
    	int sum;
    	
    	int average;
    	
    	int product;
    	
    	int smallest;
    	
    	int largest;
    
    	
    	cout << "Input three different intergers: "; 
    	
    	cin >> x >> y >> z; 
    
    	
    	sum = x + y + z; 
    	
    	cout << "Sum is " << sum << endl; 
    
    	
    	average = (x + y + z) / 3; 
    	
    	cout << "Average is " << average << endl; 
    
    	
    	product = (x * y * z); 
    	
    	cout << "Product is " << product << endl; 
    
    	
    	if(x < y && x < z)
        cout<<"Smallest: "<< x <<endl;
    
    	else if(y < x && y < z)
        cout<<"Smallest: "<< y<<endl;
    
    	else if(y < x && z < y)
        cout<<"Smallest: "<< z <<endl;

    -TP
    Last edited by compstudent; 08-17-2011 at 11:37 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Try replacing the commas with spaces.

    Jim

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Thanks Jim!!! That worked, Thank you soooo sooo much! The only issue now is my largest is not showing up (>_<)


    update:

    hahhaa. I see why my largest isn't showing up, because I don't have one..lol. XD

  4. #4
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    hahhaa. I see why my largest isn't showing up, because I don't have one..lol. XD
    How do you justify not having a largest no. among 3 ? Even if two of them are same, it should show the value.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    I fixed my problem by reading Jim's post and also fixing other coding. Thanks! Don't need anymore help. Thanks everyone! ^_^

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by compstudent View Post
    Problem solved
    That wasn't a nice thing to do. :P
    Don't you think someone else could find it useful if they had the same problem in future ?

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by compstudent View Post
    I fixed my problem by reading Jim's post and also fixing other coding. Thanks! Don't need anymore help. Thanks everyone! ^_^
    Do not edit your post once your problem is fixed. Many people donate there time here, for free, so as to help the community. You just invalidated all there work by preventing others from learning from your problem. If this is how you are going to conduct yourself, then go find a paid programming service to do your homework. Don't bother coming here anymore.

    I for one, will now go out of my way not to help you.

    EDIT: Can one of the mods restore the original post?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Quote Originally Posted by AndrewHunter View Post
    Do not edit your post once your problem is fixed. Many people donate there time here, for free, so as to help the community. You just invalidated all there work by preventing others from learning from your problem. If this is how you are going to conduct yourself, then go find a paid programming service to do your homework. Don't bother coming here anymore.

    I for one, will now go out of my way not to help you.

    EDIT: Can one of the mods restore the original post?

    I deeply sorry.. I am very new on this forum and seen it done on other forums. I didn't think it would make anyone mad. I won't come back on this site again. I just didn't want to get in trouble for plagiarism from my instructor when I posted my whole code on the forum for everyone to see. I should have just posted a certain part. Jim, I am deeply sorry because you are the one who helped me.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    As long as you are allowed to get help for your homework, there really is no way for you to get into trouble. The date and time stamp on your post as well as your account is more than enough to prove to any instructor that it is your code and that the other student copied from you. This in and of itself would prevent you from being accused of copying from anyone else.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    I will repost my question Andrew. I really am sorry. Give me 5 minutes and I'll repost my question.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Also if you really want the user to enter data like: 1,3,4 with the commas it is possible. Just get the number then the comma then the next number, etc.
    Code:
    char comma;
    cout << "Input three different intergers: "; 
    	
    cin >> x >> comma >> y >> comma >> z;
    Jim

  12. #12
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Thank you Jim. I didn't know I can do that too.

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    The extraction operator>> stops at the first non-number it encounters. Also since your variables are ints you could also use the decimal point in place of the comma as an int can not contain a decimal value.

    Jim

Popular pages Recent additions subscribe to a feed