Error With Spaces.

This is a discussion on Error With Spaces. within the C++ Programming forums, part of the General Programming Boards category; I am having some trouble with the following code. It is meant to take a string of input and store ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    45

    Error With Spaces.

    I am having some trouble with the following code. It is meant to take a string of input and store it in a character array. It should then provide some output, right aligned. This code works fine normally, however when there are spaces , ie ' 's in the input, the output that I have programmed is distorted.

    Code:
    /* main.cpp: The main C++ file for the PowerMath System */
    #include <iostream>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    char mathInput[100];
    string output;
    
    int getInput()
    {
        cout << "-> ";
        cin >> mathInput; 
        
        return 0;
    }   
    
    int processInput()
    {
        output="Function Error - No Such Function";
    }
    
    int checkExit()
    {
        char exitStr[4] = {'e','x','i','t'};
        for(int i = 0; i < 4; i++) {
                if(mathInput[i]!=exitStr[i]) { return 0; }
        }
    }
    
    int main()
    {
        cout << "PowerMath - Advanced Mathematics System\n\n";
        while(checkExit()==0)
        {
         getInput();
         processInput();
         if(checkExit()!=0) {return 0;}
         cout << setw(80) << output << "\n";
        }
        return 0;
    }
    Any suggestions folks,

    Regards,
    mintsmike

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you expect >> to read in something with spaces, it won't.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    How can I make it read in with spaces or how can I make another function read in with spaces

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    If you expect >> to read in something with spaces, it won't.
    It won't, even if you don't expect it to!

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    Ok thanks folks

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    Quote Originally Posted by mintsmike View Post
    How can I make it read in with spaces or how can I make another function read in with spaces
    Easiest way is with std::getline.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    45
    Yeah I'd already figured that out thanks (no help to people here ).

    Thanks for all of your answers though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing 3 spaces to tabs
    By dnguyen1022 in forum C Programming
    Replies: 2
    Last Post: 12-21-2008, 11:51 PM
  2. Tabs or Spaces
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 46
    Last Post: 04-08-2007, 11:45 AM
  3. saving a CString to binary file with trailing spaces
    By nineofhearts in forum C++ Programming
    Replies: 12
    Last Post: 01-03-2006, 10:53 AM
  4. Handling spaces in command line arguments
    By starkhorn in forum C++ Programming
    Replies: 7
    Last Post: 11-16-2004, 01:42 PM
  5. Replies: 5
    Last Post: 06-30-2003, 12:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21