![]() |
|
#1
|
|||
|
|||
Reading single chars from 'cin' c++I've used getline(cin, MyChar) to get the info. Now I need to read each character and then output it. If the character is a number then I output the name of the number. ie "zero" for '0'
__________________
Learning curves impede me- |
|||
|
#2
|
|||
|
|||
|
Try using cin.get or getch
|
|
#3
|
|||
|
|||
|
Quote:
If MyChar is an array of char, just use a loop to step through the chars on the line. THen for each char you could use a switch statement: CPP / C++ / C Code:
Regards, Dave |
|
#4
|
|||
|
|||
|
Here's what I have so far. I get an error for my if function. I want to see if I am at the end of my string so I can get a false and terminate the program.
CPP / C++ / C Code:
__________________
Learning curves impede me- |
|
#5
|
|||
|
|||
|
Quote:
Input is a string. What are you trying to do with this? CPP / C++ / C Code:
Now, you probably meant to use "==" instead of "=", maybe like this: CPP / C++ / C Code:
but it still does not compute. You can't compare a string with a char. Note that chars in a C++ string can be accessed by using an index, just like an array of char. That is, the i'th char of the string "Input" is Input[ i ]. Before you try to apply a bandage to this program, I suggest you step back a little and ask yourself the following question: What do you really want to do? You must answer that for yourself, but I'm guessing it's probably something like the following. (This is what I am getting from your code; I don't suggest that this is Good or Bad; you define how you want to do it.) CPP / C++ / C Code:
So: you have two loops: the outer loop reads lines until the user enters a blank line. For each no-blank line, an inner loop looks at each char in the line, and print something appropriate. If this is not what you want to do, then write it down, and tell us. Then write code that implements the outer loop (reads a line and writes it back out, and stops when the user enters a blank line). When you have done this, then attack the inner loop: step through the line a char at a time and print something for digits, something else for non-digits. Etc. Regards, Dave |
|
#6
|
|||
|
|||
|
/*Write a program that copies the characters in standard input stream, cin,
to standard output stream, cout, except for the digit characters. For a digit character, the program displays the digits name. ie: "Zero" for '0'.*/ You're right. That's what I want to do. I want my if() to ask if there are more characters or not. If no more characters, then terminate the loop. My error is that I thought Code:
Thanks for your time. __________________
Learning curves impede me- |
|
#7
|
||||
|
||||
|
You'll get better results if you change MyChar from string to char. That way you only have to deal with one character instead of an entire string containing one char. Your if statements then become
CPP / C++ / C Code:
Also, to get the benefit of Index you have to create a loop that exits when Index passes the last character of the input string. You will also benefit from looking over this information on formatting code. __________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
|
#8
|
||||
|
||||
|
Ahh, I see you do have a loop which I didn't see because of the formatting. You incremented Index but never retrieved the next character.
__________________
During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence? |
Recent GIDBlog
Review: Gel laptop cooling pad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Airport Log program using 3D linked List : problem reading from file | batrsau | C Programming Language | 11 | 29-Feb-2008 08:44 |
| Need Help reading HEX data from a sensor | Moooey | C Programming Language | 4 | 09-Mar-2005 14:46 |
| reading a char* into struct data | spike666 | C Programming Language | 7 | 19-Apr-2004 13:06 |
| Art and Maths.....a single hypervoxel | paulselhi | Graphics Forum | 0 | 10-Mar-2004 07:11 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The