GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 31-Mar-2005, 15:12
mb1 mb1 is offline
New Member
 
Join Date: Mar 2005
Location: USA
Posts: 7
mb1 is on a distinguished road

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  
Old 31-Mar-2005, 15:50
swayp swayp is offline
New Member
 
Join Date: Jan 2005
Posts: 25
swayp is on a distinguished road
Try using cin.get or getch
  #3  
Old 01-Apr-2005, 09:49
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by mb1
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'

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:
  switch(MyChar[i]) {
    case '0': cout << "zero";
    break;

    case '1': cout << "one";
    break;

...

    case '9': cout << "nine";
    break;

    default: cout MyChar[i];
  }
  cout << " "; //or whatever

Regards,

Dave
  #4  
Old 01-Apr-2005, 11:24
mb1 mb1 is offline
New Member
 
Join Date: Mar 2005
Location: USA
Posts: 7
mb1 is on a distinguished road
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:
/*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'.*/

#include <iostream>
#include <conio.h>
#include <string>
using namespace std;

int main()
{
	//Step01: Declare Memory Locations
	string InputLine;
	string Input;
	//Character position
	
	bool Continue = true;
	//Step02: Initialize Memory Locations
	int Index = 0;
	string MyChar = Input.substr(Index, 1);
	//Step03: Do the Work
	//Step03a: Ask for the characters
	cout << "Enter characters: ";
	cin >> Input;
	//Step03b: Output the characters
	//getline(cin, Input, '\n');
	while(Continue)
	{if(Input = '\n')
	{Continue = false;}
	else if(MyChar == "0")
		{cout << "zero" << endl;
		Index++;}	
	else if(MyChar == "1")
		{cout << "one" << endl;
		Index++;}
	else if(MyChar == "2")
		{cout << "two" << endl;
		Index++;}
	else if(MyChar == "3")
		{cout << "three" << endl;
		Index++;}
	else if(MyChar == "4")
		{cout << "four" << endl;
		Index++;}
	else if(MyChar == "5")
		{cout << "five" << endl;
		Index++;}
	else if(MyChar == "6")
		{cout << "six" << endl;
		Index++;}
	else if(MyChar == "7")
		{cout << "seven" << endl;
		Index++;}
	else if(MyChar == "8")
		{cout << "eight" << endl;
		Index++;}
	else if(MyChar == "9")
		{cout << "nine" << endl;
		Index++;}
	else 
	 	{cout << MyChar << endl;
	 	Index++;}

	}
	//Step04: Wrap and Exit
	cout << endl;
	getch();
	return 0;
}
__________________
Learning curves impede me-
  #5  
Old 01-Apr-2005, 12:03
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by mb1
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.

Input is a string. What are you trying to do with this?
CPP / C++ / C Code:
if(Input = '\n')
This tries to set the string Input to the char '\n' (which is what your compiler is complaining about).

Now, you probably meant to use "==" instead of "=", maybe like this:
CPP / C++ / C Code:
if(Input == '\n')

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:
  continue = TRUE;
  cin.getline(...
  while(continue) {
    //If it is a blank line, set continue to FALSE
   // If it is not a blank line then step through the input string
   // a char at a time.
    // if the char is '0', '1', '2',...'9' print out the string "zero", or "one", or ...
   // if the char is not a digit, print out the char

   // after processing the complete line, then get the next line
    cin.getline(...
  }



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  
Old 01-Apr-2005, 12:11
mb1 mb1 is offline
New Member
 
Join Date: Mar 2005
Location: USA
Posts: 7
mb1 is on a distinguished road
/*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:
if(Input = '\n')
asked if the program had another character to check or not.

Thanks for your time.
__________________
Learning curves impede me-
  #7  
Old 01-Apr-2005, 12:17
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
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:
case '0':
, or use a switch() statement.

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.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
  #8  
Old 01-Apr-2005, 12:22
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,373
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
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.
__________________

The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
 
 

Recent GIDBlogProblems with the Navy (Enlisted) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

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 07:44
Need Help reading HEX data from a sensor Moooey C Programming Language 4 09-Mar-2005 13:46
reading a char* into struct data spike666 C Programming Language 7 19-Apr-2004 12:06
Art and Maths.....a single hypervoxel paulselhi Graphics Forum 0 10-Mar-2004 06:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:57.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.