C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 1st, 2004, 06:16 PM
amangarg2310 amangarg2310 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 40 amangarg2310 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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?

Reply With Quote
  #2  
Old March 1st, 2004, 06:27 PM
nunki's Avatar
nunki nunki is offline
Checked Google ;)
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Phoenix, Arizona
Posts: 231 nunki User rank is Private First Class (20 - 50 Reputation Level)nunki User rank is Private First Class (20 - 50 Reputation Level) 
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

Reply With Quote
  #3  
Old March 1st, 2004, 06:28 PM
amangarg2310 amangarg2310 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 40 amangarg2310 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #4  
Old March 1st, 2004, 06:40 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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?

Reply With Quote
  #5  
Old March 1st, 2004, 06:44 PM
amangarg2310 amangarg2310 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 40 amangarg2310 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #6  
Old March 1st, 2004, 06:45 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Renaissance Redneck
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,511 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 26 m 26 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
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.

Reply With Quote
  #7  
Old March 1st, 2004, 06:50 PM
amangarg2310 amangarg2310 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 40 amangarg2310 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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!

Reply With Quote
  #8  
Old March 1st, 2004, 06:50 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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';
....

Reply With Quote
  #9  
Old March 1st, 2004, 06:56 PM
Lux Perpetua Lux Perpetua is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Feb 2004
Location: San Francisco Bay
Posts: 1,936 Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level)Lux Perpetua User rank is General 5th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 2 h 12 m 42 sec
Reputation Power: 1312
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.

Reply With Quote
  #10  
Old March 1st, 2004, 06:57 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Renaissance Redneck
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,511 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 26 m 26 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
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 :-).

Reply With Quote
  #11  
Old March 1st, 2004, 07:02 PM
DaWei_M's Avatar
DaWei_M DaWei_M is offline
Renaissance Redneck
Dev Shed God 8th Plane (8500 - 8999 posts)
 
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,511 DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level)DaWei_M User rank is General 34th Grade (Above 100000 Reputation Level) 
Time spent in forums: 4 Weeks 18 h 26 m 26 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
I personally just never use cin / cout . (Do NOT, I repeat, NOT look at my posts!

Reply With Quote
  #12  
Old March 1st, 2004, 08:51 PM
amangarg2310 amangarg2310 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 40 amangarg2310 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #13  
Old March 1st, 2004, 10:16 PM
nunki's Avatar
nunki nunki is offline
Checked Google ;)
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Phoenix, Arizona
Posts: 231 nunki User rank is Private First Class (20 - 50 Reputation Level)nunki User rank is Private First Class (20 - 50 Reputation Level) 
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

Reply With Quote
  #14  
Old March 2nd, 2004, 09:01 PM
RapperMas RapperMas is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2004
Posts: 9 RapperMas User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
  #15  
Old March 2nd, 2004, 09:19 PM
jubitzu jubitzu is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Colorful Colorado
Posts: 743 jubitzu User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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;
}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > how to read individual characters

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap