The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
how to read individual characters
Discuss how to read individual characters in the C Programming forum on Dev Shed. how to read individual characters C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 1st, 2004, 06:16 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 40
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
how to read individual characters
if the user inputs the number 12345
how would i calculate 1+2+3+4+5
Basically my question is: is there a way for the c program to read the number 12345 seperately as 1,2,3,4 and 5?
would i use cin.get()?
I tried the while statement: while ( (digit = cin.get() ) number <=5) {
but that doesnt work
can someone help me?
|

March 1st, 2004, 06:27 PM
|
 |
Checked Google ;)
|
|
Join Date: Dec 2003
Location: Phoenix, Arizona
Posts: 231

Time spent in forums: 2 Days 1 h 11 m 6 sec
Reputation Power: 10
|
|
|
read in the whole line as text and then parse the string. Make sure each char is a digit then keep a running total.
__________________
No, I'm from Iowa. I just work in outer space.
-- James T. Kirk
|

March 1st, 2004, 06:28 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 40
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
im sorry
im still a beginner and i have no idea what ur talking about!
please clarify
|

March 1st, 2004, 06:40 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
You say "c program", but cin is a C++ feature that isn't in C. In C++, you would use cin.get() to get individual characters; in C, you would use getc(stdin) or getchar(). (For practical purposes, the C/C++ variants have the same functionality; it's just the name that's different.)
Also, Quote: | ( (digit = cin.get() ) number <=5) | isn't a valid C++ (or C) expression. What is "number" supposed to be?
|

March 1st, 2004, 06:44 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 40
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
this is my source
// Lab6a
//Program that uses a counter-controlled while loop to input the 5
//digits of a 5 digit number (assume no spaces will appear between
//digits) and then prints the sum of the digits
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main ()
{
int numbers = 1;
int sum;
int A;
int B;
int C;
int D;
int E;
while ( numbers <= 5 ) {
cout << "Enter a five digit number: " <<endl;
cin >> A;
numbers = numbers +1;
cin >> B;
numbers = numbers +1;
cin >> C;
numbers = numbers +1;
cin >> D;
numbers = numbers +1;
cin >> E;
numbers = numbers +1;
sum = A + B + C + D + E;
cout << " sum is "<< sum << endl;
}
return 0;
}
|

March 1st, 2004, 06:45 PM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
Nunki means to read in the whole thing so you have a string of characters, 12345. Then point a pointer at the first one, convert it to a number (it's a code representing a character digit), add it to a total that starts off at zero, then point to the next one and do the same thing. Continue until you run out of stuff to process, and you'll have a running total. Refer to a ASCII table to see what actual numeric value the character codes have. '1', for instance, is 49, '2' is 50, and so forth. You'll have to subtract out that offset to get the actual quantitative value. You might want to review the "Convert a character to binary" thread.
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.
|

March 1st, 2004, 06:50 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 40
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
sorry
Isnt there an easier way...im still not understanding what ur talking about. I am only a beginner. Thanks for ur effort and help though, A LOT!
|

March 1st, 2004, 06:50 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
amangarg,
There's no need to use a while loop, since it always loops exactly once.
The line
cin >> A;
will read the entire 5-digit number into A. You need to use cin.get(), not the >> operator, to get individual characters:
Code:
A = cin.get() - '0';
B = cin.get() - '0';
....
|

March 1st, 2004, 06:56 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Location: San Francisco Bay
|
|
Quote: | Originally Posted by amangarg2310 Isnt there an easier way...im still not understanding what ur talking about. I am only a beginner. Thanks for ur effort and help though, A LOT! | They were suggesting a more powerful and general paradigm for processing input. Frankly, in your situation, it's big-time overkill, since the normal cin functions do what you want. If I were you, I would forget about what they said until you have your feet firmly on the ground of C++ (which you will); you'll get to it eventually.
|

March 1st, 2004, 06:57 PM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
Lux is showing you the best looking and easiest way to do it. So listen and pay attention instead of whining in between posts. That should be fine to turn in. If someone asks you what happens if they type in 12Z45, just tell 'em you dunno, you're working on that :-).
|

March 1st, 2004, 07:02 PM
|
 |
Renaissance Redneck
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
I personally just never use cin / cout  . (Do NOT, I repeat, NOT look at my posts! 
|

March 1st, 2004, 08:51 PM
|
|
Contributing User
|
|
Join Date: Feb 2004
Posts: 40
Time spent in forums: < 1 sec
Reputation Power: 10
|
|
|
Is this what u mean?
Actually I need a while loop in the program as the program assignment. THe following program still does not work
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main ()
{
int numbers = 1;
int sum;
char digit;
while ( (digit = cin.get() ) numbers <= 5 ) {
cout << "Enter a five digit number: " <<endl;
cin >> digit;
numbers = numbers +1;
cin >> digit;
numbers = numbers +1;
cin >> digit;
numbers = numbers +1;
cin >> digit;
numbers = numbers +1;
cin >> digit;
numbers = numbers +1;
sum = digit + digit + digit + digit + digit;
cout << " sum is "<< sum << endl;
}
return 0;
}
|

March 1st, 2004, 10:16 PM
|
 |
Checked Google ;)
|
|
Join Date: Dec 2003
Location: Phoenix, Arizona
Posts: 231

Time spent in forums: 2 Days 1 h 11 m 6 sec
Reputation Power: 10
|
|
You need to be adding to sum after every cin i.e. after you increment numbers add to sum. Secondly put the cout above the while, and the while loop should only run to 5. Ditch digit = cin.get(). Your on the right track, just some minor syntax and thought glitches. When you do cin>>digit, it overwrites what was in it before right? So thats why you need to take that value and add it to the running sum, because when you do another cin>>digit, it wipes out the old value, or as DaWei_M would say "the old value goes right to davy jones locker".
On another note I would recommend you take advantage of the while loop. You really only need to grab a char once in the while loop and increment the counter, because as it stands your while loop only runs once.
Something like
Code:
int counter=0;
while(counter<5)
{
cin>>digit;
//add to sum
counter++;
}
Last edited by nunki : March 1st, 2004 at 10:30 PM.
Reason: while loop
|

March 2nd, 2004, 09:01 PM
|
|
Registered User
|
|
Join Date: Mar 2004
Posts: 9
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
This isn't too efficient. First of all, the amount of letters have to be declared in the beginning of the program. Second, the user has to enter each digit seperately, thus making this program a strong proponent of great confusion. The user will not think of this as very clever. I will post this code exactly 1 day later.
Quote: | Originally Posted by amangarg2310 // Lab6a
//Program that uses a counter-controlled while loop to input the 5
//digits of a 5 digit number (assume no spaces will appear between
//digits) and then prints the sum of the digits
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
int main ()
{
int numbers = 1;
int sum;
int A;
int B;
int C;
int D;
int E;
while ( numbers <= 5 ) {
cout << "Enter a five digit number: " <<endl;
cin >> A;
numbers = numbers +1;
cin >> B;
numbers = numbers +1;
cin >> C;
numbers = numbers +1;
cin >> D;
numbers = numbers +1;
cin >> E;
numbers = numbers +1;
sum = A + B + C + D + E;
cout << " sum is "<< sum << endl;
}
return 0;
} |
|

March 2nd, 2004, 09:19 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Colorful Colorado
Posts: 743
Time spent in forums: 21 h 16 m 10 sec
Warnings Level: 10
Number of bans: 1
Reputation Power: 0
|
|
this is probably the solution you need. i would just like to point out that this is the second assignment for which you will be turning in someone else's work. are you learning anything?
Code:
#include <iostream>
using namespace std;
int main()
{
int s;
char n;
cout << "Enter number: ";
n = cin.get();
while (n >= '0' && n <= '9')
{
s += n - '0';
n = cin.get();
}
cout << "Sum of digits: " << s << endl;
return 0;
}
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|