Learning C++ need help

Status
Not open for further replies.

PeopLes

Solid State Member
Messages
11
I am in a c++ programming class and I don't want to wait to get help from the teacher with this exercise i am hoping somebody here could help with this exercise...


"Write a program that displays the sum of the digits of any non negative interger. for example

Enter an interger: 145
Sum of digits is 10

Enter an interger: 8888
Sum of digits is 32

Could somebody please help and remember I am just beginning c++ please dont make it too complicated
 
i was trying to write your program, but i am getting stuck probably the same place you are at.

1) i do not know how to calculate the input unless the user uses spaces between each integer, example: 2 4 6 2 as opposed to 2462.

2) i can't figure out how you would determine if the number entered is negative so the program can warn the user that they inputted a negative number.

so far my best guess is that we need to use control structures which is still basic but maybe ahead of where you and i are learning.

this should be an easy program to write, but i'm stumped. i have a bunch of non-working code that would be pointless to post here. i'm very interested in the answer as well, someone help
 
after reading ahead i saw something on the header file <string.h> and its function string.length. so maybe if we have the program treat the integer inputted as a string and we get the length of the string we can then have the program make as many variables as the length of the string. your problem is more advanced than it seems now.

PeopLes i'm sure you have your answer by now from your teacher, can you post the code here please, i'm really interested to know.
 
This is a messy way of doing it but it works
i separated each column of numbers in if statements up to the
maximum amount of numbers alowable using the long variable
i also enclosed it in a while loop where 0 is pressed
to quit, and an error is generated if a negative number is entered



void exercise8(){



long lNumber;

system("cls");

cout<<"Exercise 8\n\n";


cout <<"Enter a positive interger (0 to Quit): ";
cin >>lNumber;

while(lNumber != 0){


if((lNumber >= 10)&&(lNumber < 99))
cout <<"\n\t\t\t" <<((lNumber / 10) + (lNumber % 10));

if((lNumber >= 100)&&(lNumber < 999))
cout <<"\n\t\t\t" <<(lNumber / 100) + (lNumber / 10 % 10 + lNumber % 10);

if((lNumber >= 1000)&&(lNumber < 9999))
cout <<"\n\t\t\t" <<(lNumber / 1000) + (lNumber / 100 % 10) + (lNumber / 10 % 10) + (lNumber % 10);

if((lNumber >= 10000)&&(lNumber < 99999))
cout <<"\n\t\t\t" <<(lNumber / 10000) + (lNumber / 1000 % 10) + (lNumber / 100 % 10) + (lNumber / 10 % 10) +
(lNumber % 10);

if((lNumber >= 100000)&&(lNumber < 999999))
cout <<"\n\t\t\t" <<(lNumber / 100000) + (lNumber / 10000 % 10) + (lNumber / 1000 % 10) +
(lNumber / 100 % 10) + (lNumber / 10 % 10) + (lNumber % 10);

if((lNumber >= 1000000)&&(lNumber < 9999999))
cout <<"\n\t\t\t" <<(lNumber / 1000000) + (lNumber / 100000 % 10) + (lNumber / 10000 % 10) +
(lNumber / 1000 % 10) + (lNumber / 100 % 10) + (lNumber / 10 % 10) + (lNumber % 10);

if((lNumber >= 10000000)&&(lNumber < 99999999))
cout <<(lNumber / 10000000) + (lNumber / 1000000 % 10) + (lNumber / 100000 % 10) + (lNumber / 10000 % 10)+
(lNumber / 1000 % 10) + (lNumber / 100 % 10) + (lNumber / 10 % 10) + (lNumber % 10);

if((lNumber >= 100000000)&&(lNumber < 999999999))
cout <<"\n\t\t\t" <<(lNumber / 100000000) + (lNumber / 10000000 % 10) + (lNumber / 1000000 % 10) +
(lNumber / 100000 % 10) + (lNumber / 10000 % 10) + (lNumber / 1000 % 10) +
(lNumber / 100 % 10) + (lNumber / 10 % 10) + (lNumber % 10);

if((lNumber >= 1000000000)&&(lNumber < 9999999999))
cout <<"\n\t\t\t" <<(lNumber / 1000000000) + (lNumber / 100000000 % 10) + (lNumber / 10000000 % 10) +
(lNumber / 1000000 % 10) + (lNumber / 100000 % 10) + (lNumber / 10000 % 10) +
(lNumber / 1000 % 10) + (lNumber / 100 % 10) + (lNumber / 10 % 10) + (lNumber % 10);
else if(lNumber < 0)
cout <<"\n\t\t\t::: Must Be A Positive Interger :::\n";

cout <<"\n\nEnter a positive interger (0 to Quit): ";
cin >>lNumber;

}
 
the following code can be more readable...

cout<<"Enter Number : ";
cin>>n;
n1 = n;

while(n1!=0)
{
sum = n1%10;
n1/=10;
}

cout<<sum;

the logic to check if it is a +ve number can be used here
 
i'm trying to learn C++ also, and i thought i'd try to make this program. i'm trying to make the program record the number the user enters in an array, and then add all the values in the array together. it would be a lot cleaner than the one PeopLes gave if i could get it to work, but i think i'm either not understanding how arrays work or how the cin.getline command works. the only way i can get the program to compile successfully is if the program records the user's number as a character string instead of an array, but then the output is all messed up (e.g.: if u enter 1 it gives u 262, if u enter 11 it gives u 227). can someone help me out? here's the program that compiles successfully:

#include <iostream.h>
#include <stdlib.h>

int AddDigits (int a, int b, int c, int d, int e, int f, int g, int h, int i, int j)
{
int sum;
sum=a + b + c + d + e + f + g + h + i + j;
return (sum);
}
int main()
{
char number[10];
int a, b, c, d, e, f, g, h, i, j, sum;
cout <<"Enter a number:";
cin.getline (number, 10);
a=number[1];
b=number[2];
c=number[3];
d=number[4];
e=number[5];
f=number[6];
g=number[7];
h=number[8];
i=number[9];
j=number[10];
sum=AddDigits(a, b, c, d, e, f, g, h, i, j);
cout <<"Number: " <<number <<endl <<"Sum of digits: " <<sum;
return 0;
}
 
Well...looks like there's a problem with data types here...

char is just 1 byte and int is either 2 byte or 4 byte depending on the compiler you use

when you assign a char to int, the char is assigned to the lower byte of the int and rest of the byte(s) are filled by junk value

this messes up the int number and that's y u get wrong results or you could re-write your add function to take in character values

you can try using a for loop to get input
 
Status
Not open for further replies.
Back
Top Bottom