C++ Help Me!!!!!!!!

This is a discussion on C++ Help Me!!!!!!!! within the C++ Programming forums, part of the General Programming Boards category; I need to take a 3 digit user input and add each individual number in the digit together......its a crack ...

  1. #1
    Hudy23
    Guest

    Post C++ Help Me!!!!!!!!

    I need to take a 3 digit user input and add each individual number in the digit together......its a crack head program but its for school....now im not asking anyone to do my homework for me however i dont know where to start....im confused and aggitated!!!

  2. #2
    Unregistered
    Guest
    Code:
    int main(){
      cout<<"Enter three numbers: ";
      int num1, num2, num3;
      cin>>num1>>num2>>num3;
      
      cout<<num1+num2+num3<<endl;
      return 0;
    }
    or you could enter the three numbers as a string and then add each element by using atoi
    [code]
    int main(){
    cout<<Enter three numbers: ";
    char *s;
    cin.getline(s);cin.ignore();

    int num1, num2, num3;
    num1=atoi(s[0]);num2=atoi(s[1]);num3=atoi(s[2]);
    cout<<num1+num2+num3<<endl;
    return 0;
    }

  3. #3
    Unregistered
    Guest
    sorry, it was supposed to look like this
    Code:
    int main(){ 
      cout<<Enter three numbers: "; 
      char *s; 
      cin.getline(s);cin.ignore(); 
    
      int num1, num2, num3; 
      num1=atoi(s[0]);num2=atoi(s[1]);num3=atoi(s[2]); 
      cout<<num1+num2+num3<<endl; 
      return 0; 
    }

  4. #4
    Unregistered
    Guest
    Hey,
    whoever repiled to this the first and second time, your replies are the same, and the program doesnt run.(it has warnings that crash it)
    Thanks anyway

  5. #5
    geek SilentStrike's Avatar
    Join Date
    Aug 2001
    Location
    NJ
    Posts
    1,141
    Put this at the top of that program and it should work fine,

    Code:
    #include <iostream>
    using namespace std;
    Prove you can code in C++ or C# at TopCoder, referrer rrenaud
    Read my livejournal

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    this code is slightly wrong......
    Code:
    int main(){ 
      cout<<Enter three numbers: "; 
      char *s; 
      cin.getline(s);cin.ignore(); 
    
      int num1, num2, num3; 
      num1=atoi(s[0]);num2=atoi(s[1]);num3=atoi(s[2]); 
      cout<<num1+num2+num3<<endl; 
      return 0; 
    }
    The arguements to getline are wrong and you attempt to input into memory you dont 'own'.
    here is the same piece of code with fixes....
    Code:
    #include<iostream>
    using namespace std;
    int main(){ 
      cout<<Enter three numbers: "; 
      char s[81] 
      cin.getline(s,sizeof(s));cin.ignore(); 
    
      int num1, num2, num3; 
      num1=atoi(s[0]);num2=atoi(s[1]);num3=atoi(s[2]); 
      cout<<num1+num2+num3<<endl; 
      return 0; 
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Sep 2001
    Posts
    44

    Exclamation your all wrong

    You guys are doing it all wrong. He said 3 DIGIT number, and then add each individual digit. so if you entered 125, you would add 1+2+5 wich equals 8.

    However, I do not have time to explain the code. So hopefully you guys can work off of this. If I had a few more minutes, I'd write the code for you. Sorry!
    [Strut]

    I lay on my bed watching the stars, and I thought to myself... Where the hell is my roof?

  8. #8
    Satan valar_king's Avatar
    Join Date
    Aug 2001
    Posts
    288
    how about getting the input a an array of integers and adding the integers?
    -Save the whales. Collect the whole set.

  9. #9
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you can't get the input as seperate numbers or in an array then you can seperate the individual digits using the / and % operators (divide by 100 to get first digit and get remainder using %, divide remainder by 10 to get second digit..etc).

  10. #10
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    a three digit number is a meaningless concept to the compiler. The input could be stored as a string, an int, a double, a float, or any number of variable types. If the requirement was to enter an integer consisting of three digits, then the concepts as presented so far would be off base.

    char sEntry[4];
    int entry;
    cout << "enter a 3 digit number" << endl;
    cin >> sEntry;
    cout << "enter another 3 digit number" << endl;
    cin >> entry;

    However, here's a completely different approach that doesn't require conversion from int to string or char to int or any of that stuff:

    int num1, num2, num3, i, temp;
    int total = 0;
    temp = entry;

    num3 = temp % 10;
    temp = temp/10;//this is integer math, not routine math//
    num2 = temp % 10;
    num1 = temp/10;
    total = num1 + num2 + num3;
    cout << "the sum of the digits of the number " << entry << " is " << total << "." << endl;

    Oops, looks like Zen beat me to the punch as I was waylaid in my posting my response. Oh well.
    Last edited by guest; 09-19-2001 at 04:46 PM.

Popular pages Recent additions subscribe to a feed

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