Reading white spaces? oh and scanf_s

This is a discussion on Reading white spaces? oh and scanf_s within the C++ Programming forums, part of the General Programming Boards category; How do you get user input including white space? say, using scanf? Oh and I can't get scanf_s to work.. ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    67

    Reading white spaces? oh and scanf_s

    How do you get user input including white space? say, using scanf?
    Oh and I can't get scanf_s to work.. it never reads properly.. weird.
    And is there a way to only read a certain number of characters based on a variable?

    like scanf("%%us",sizeof(string),string); but of course that looks for the input
    "%us"

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should read the manual on scanf. Things like scansets, and the * modifier.... Edit: And maybe we should be in the C forum. And of course, you could read the manual on fgets just as well.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    oh I'm using it in C++.. whoops.
    yeah I sort of figured it out.. the MSDN entries on scanf hurt my head T_T.
    I think I got it though.. using stuff like either
    Code:
    scanf("%s %s %s",...);
    or
    scanf("%[ ]");

  4. #4
    Registered User
    Join Date
    Mar 2008
    Posts
    5
    i think getline method of istream class(e.g. cin is instantiation of this class) would be useful for such issue.

    example from cplusplus.com
    Code:
    // istream getline
    #include <iostream>
    using namespace std;
    
    int main () {
      char name[256], title[256];
    
      cout << "Enter your name: ";
      cin.getline (name,256);
    
      cout << "Enter your favourite movie: ";
      cin.getline (title,256);
    
      cout << name << "'s favourite movie is " << title;
    
      return 0;
    }

  5. #5
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    yeah it is.. but actually I'm trying to get a specific number of statements, so scanf works better.
    in general though, yeah getline would be better.
    thanks

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Why not use istream and have a comma ',' delimit each statement then do a simple string parse to get the input?

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,170
    Code:
    // istream getline
    #include <iostream>
    using namespace std;
    
    int main ()
    {
        std::string name, title;
    
        cout << "Enter your name: ";
        std::getline(name/*, ","*/);
    
        cout << "Enter your favourite movie: ";
        std::cin(title/*, ","*/);
    
        cout << name << "'s favourite movie is " << title;
    
        return 0;
    }
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  8. #8
    Registered User
    Join Date
    Sep 2007
    Posts
    67
    the space thing is working fine, but thanks for the suggestion

Popular pages Recent additions subscribe to a feed

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