473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Odd issue with white space.

Hey, I've got a C++ program that's been stumping me all week. The assignment called for a program that converts a phrase/string into corresponding telephone digits (2 for a, b, and c, etc.). That part wasn't a problem. What's been confusing me is that when there's a space in the input, the program stop reading the string (i.e, if the input is 'the root', it will process the 'the', and then run the exit loop. Here's the code so that hopefully one of you guys can give me a hand.

Thanks, J.

Expand|Select|Wrap|Line Numbers
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9.  
  10.     char cExit;
  11.     int iPosition;
  12.     string sWord;
  13.     bool bExit, bError;
  14.  
  15.     cout << "Welcome to the Telephone Phrase Converter." << endl;  
  16.  
  17.     do
  18.     {
  19.      cout << "Please input a phrase to be converted into a telephone"
  20.      << " number." << endl;
  21.      cin >> sWord;
  22.      string sLetter = "";
  23.  
  24.          for (iPosition = 0; iPosition <= 6; iPosition++)
  25.          {  
  26.                   /* if (iPosition == 3)
  27.                       {
  28.                       sLetter += '-'; 
  29.                       }*/              
  30.                 switch(sWord[iPosition])
  31.                 {    
  32.                 case ' ':
  33.                 sLetter += " ";
  34.                       break;
  35.  
  36.                 case 'a':
  37.                 case 'A':
  38.                 case 'b':
  39.                 case 'B':
  40.                 case 'c':
  41.                 case 'C':
  42.                       sLetter += '2';
  43.                       break;
  44.                 case 'd':
  45.                 case 'D':
  46.                 case 'e':
  47.                 case 'E':
  48.                 case 'f':
  49.                 case 'F':
  50.                       sLetter += '3';
  51.                       break;
  52.                 case 'g':
  53.                 case 'G':
  54.                 case 'h':
  55.                 case 'H':
  56.                 case 'i':
  57.                 case 'I':
  58.                       sLetter += '4';
  59.                       break;
  60.                 case 'j':
  61.                 case 'J':
  62.                 case 'k':
  63.                 case 'K':
  64.                 case 'l':
  65.                 case 'L':
  66.                       sLetter += '5';
  67.                       break; 
  68.                 case 'm':
  69.                 case 'M':
  70.                 case 'n':
  71.                 case 'N':
  72.                 case 'o':
  73.                 case 'O':
  74.                       sLetter += '6';
  75.                       break;
  76.                 case 'p':
  77.                 case 'P':
  78.                 case 'q':
  79.                 case 'Q':
  80.                 case 'r':
  81.                 case 'R':
  82.                 case 's':
  83.                 case 'S':
  84.                       sLetter += '7';
  85.                       break;
  86.                 case 't':
  87.                 case 'T':
  88.                 case 'u':
  89.                 case 'U':
  90.                 case 'v':
  91.                 case 'V':
  92.                       sLetter += '8';
  93.                       break;
  94.                 case 'w':
  95.                 case 'W':
  96.                 case 'x':
  97.                 case 'X':
  98.                 case 'y':
  99.                 case 'Y':
  100.                 case 'z':
  101.                 case 'Z':
  102.                      sLetter += '9';
  103.                      break;
  104.                 }
  105.                 /* This switch checks the character*/
  106.          }   
  107.  
  108.      cout << sLetter << endl; //Prints the assembled string
  109.  
  110.      do
  111.      {
  112.      cout << "Would you like to convert another phrase? (y/n)" << endl;
  113.      cin >> cExit;               
  114.  
  115.            if (cExit == 'y' || cExit == 'Y')
  116.            {
  117.             bExit = false;
  118.             bError = false;
  119.            }
  120.            else if (cExit == 'n' || cExit == 'N')
  121.            {
  122.             bExit = true;
  123.             bError = false;
  124.            }
  125.            else
  126.            {
  127.            bError = true;
  128.            }
  129.      }
  130.      while (bError != false);
  131.     }
  132.     while (bExit == false);
  133.  
  134. return 0;
  135. }
Apr 5 '07 #1
2 1612
Ganon11
3,652 Expert 2GB
It's because you are using cin >> sWord. cin skips all whitespace, and also stops input based on whitespace. So if you enter "the root", cin will take "the" first, then "root". You should use getline(cin, sWord) in order to get all the input from the beginning to the enter key.
Apr 5 '07 #2
It's because you are using cin >> sWord. cin skips all whitespace, and also stops input based on whitespace. So if you enter "the root", cin will take "the" first, then "root". You should use getline(cin, sWord) in order to get all the input from the beginning to the enter key.
That worked! Thanks a lot!
Apr 5 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Grumble | last post by:
Hello all, I want to read lines from a text file, where each line has the following syntax: token1:token2:token3 There could be white space between tokens and ':'
2
by: Malcolm Dew-Jones | last post by:
I am looking at xslt 1.0 and trying to understand if empty text nodes are supposed to be stripped or not as the default behaviour. 3.4 starts by listing rules for when white space is not stripped...
17
by: Stanimir Stamenkov | last post by:
Is it possible to make two inline elements to appear adjacent stripping any white space appearing in between in the source? Example: <span class="adj">1</span> <span class="adj">2</span>...
5
by: Applebrown | last post by:
Hello, basically, I'm just learning intermediate CSS and trying to convert my old table webpage completely to CSS. Hoorah, right? Well, it's not quite going as planned. It's an extremely simple...
0
by: Mark Moore | last post by:
I'm trying to layout a couple text input fields and their corresponding labels without using a table. When I was trying to debug my understanding of CSS, I was *very* surprised to see that span's...
6
by: michaelzap | last post by:
I can't seem to remove some extra margin/padding from my design on this site: http://www.kyowa-usa.com/ IE displays it properly (no space between the menus and other elements and the outer...
38
by: Xah Lee | last post by:
sometimes i wish to add white space in <p> as to achived effects similar to tab. what should i do? using empty image seems the sure way but rather complicated. (and dosen't change size with...
2
by: shagy | last post by:
Hi, I'm having a problem with a <select><option> which has white space in values... When I post the data I only get the first word (up to the white space). "Testing white space" becomes...
4
by: mosesdinakaran | last post by:
Can any one explain how the rule is applied for the following Regular expression $Str = 'the red king'; $Pattern = '/((red|white) (king|queen))/'; preg_match($Pattern,$Str,$Val); Result:
1
by: =?Utf-8?B?UmV5?= | last post by:
I have a textbox and linked that with the autocomplete extender I have created the webservice and the WebMethod and returns a ToArray to the textbox. Everything works when I type normal string...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.