How to include spaces in c++

This is a discussion on How to include spaces in c++ within the C++ Programming forums, part of the General Programming Boards category; hello im a bit of a newbie and i have a program in c++ and it asks questions, where u ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    20

    Question How to include spaces in c++

    hello
    im a bit of a newbie and i have a program in c++ and it asks questions, where u live, etc. when i type in an answer with a space in it, it skips the following question.
    does anyone know how to combat this problem?
    thanks in advance

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    If you're using scanf(), it's because it stops reading up to the next space (cin may do that too). Use fgets(), or cin.getline().
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    20
    sorry i dont understand what you mean

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Well, I can't read your mind, why not post the problem code?
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    20
    Code:
    #include <iostream> // This list defines what 'extras' are to be used within the program
    #include <string>
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    using namespace std;
    
    int main ( ) {
        char manager[20]; // This list assigns the indentifiers with data types
        char products[5][20];
        float prices[5];
        char suburb [20];
        float Total = 0; // Defines Total's current value as '0'
        char state [20];
        int     items, num;
    	char terminalid [8];
    	char eftpos [20];
        char cardnumber [20];
        char enter [24];
        
        cout<<"Welcome to the Receipt Generator" << endl;
        cout<<"Press any key and enter to begin..."; // This feature allows the user to choose when he/she is ready to start the program
        cin>>enter;
        system("cls"); // This command clears the screen for a more user-friendly appearance
        cout<<"Enter the suburb of the store (upper case and no spaces please) >    "; 
        cin>> suburb; // The user enters the suburb with the knowledge that it will be printed on the receipt
        cout<<"Enter the state that the  store is in (Upper case) > ";
        cin>> state; // The user enters the state
    This is the beggining part of it. I get problems when i want to enter spaces in the answers

  6. #6
    Registered User
    Join Date
    Nov 2002
    Posts
    20
    ive had to write no spaces please because of the systems inability to handle spaces in the answers

  7. #7
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    Both cin and scanf use a space as a terminator, so it will only read up to the space. To get past this use cin.getline().

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    cin.getline(suburb, 20, '\n'); // << safest

    or:

    cin.getline(suburb, '\n');
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  9. #9
    Spanky devour89's Avatar
    Join Date
    Nov 2002
    Posts
    148
    I agree with sabastian

    use

    Code:
    cin.getline(name, 20, '\n')
    this will write the whole typed string into name
    Dev C++
    Win XP/2k/98

    I DO NOT TAKE CLASSES I DONT GET HOMEWORK THIS IS NOT A HOMEWORK QUESTION!!!

    He's lean he's keen... He's the spank machine!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to open include file SDL_audio.h
    By rraj.be in forum C Programming
    Replies: 2
    Last Post: 06-28-2008, 08:04 PM
  2. Problem while constructing IP packet and sending using socket() system call
    By cavestine in forum Networking/Device Communication
    Replies: 10
    Last Post: 10-15-2007, 05:49 AM
  3. include problem
    By Strait in forum C++ Programming
    Replies: 4
    Last Post: 01-31-2005, 03:01 PM
  4. Singleton problems
    By techrolla in forum C++ Programming
    Replies: 16
    Last Post: 01-02-2005, 03:05 PM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM

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