Formatted input

This is a discussion on Formatted input within the C++ Programming forums, part of the General Programming Boards category; I want to input two variables like a/b.One book suggest this as Code: int a,b; char dummychar; cin>>a>>dummychar>>b; Can anyone ...

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    113

    Formatted input

    I want to input two variables like a/b.One book suggest this as
    Code:
    int a,b;
    char dummychar;
    cin>>a>>dummychar>>b;
    Can anyone explain why above code gives the desired result.Also is there any other way out.In C we used to do it as

    Code:
    scanf("%d/%d",&a,&b);
    Also please give me a link to other aspect of formatted input in C++.
    Last edited by vaibhav; 10-22-2005 at 03:04 AM.

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    Because '/' is a character and you can't read in a character into an integer. If you did the stream would fail.

    Code:
    cin >> a >> b;
    // a = value of a
    // The stream fails at second input and all cin statements 
    // are ignored for the rest of the program
    The dummy character is there to read the input that you don't want.
    Sent from my iPadŽ

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 01:20 PM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 12:22 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM

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