cin question

This is a discussion on cin question within the C++ Programming forums, part of the General Programming Boards category; I have some problems using cin -command. Let's assume someone writes a random input from keyboard and it is "123.04.00012" ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    5

    cin question

    I have some problems using cin -command. Let's assume someone writes a random input from keyboard and it is "123.04.00012" + <return>, how can I store numbers 123, 4 and 12 to variables? I'd like to handle "." as a separator. Please give me a simple answer.

  2. #2
    Seņor Member
    Join Date
    Jan 2002
    Posts
    561
    use cin.get() I think.

  3. #3
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    Maybe you can use this.

    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <conio.h>
    
    int main(void)
    {
    char str[50];
    cin >> str;
    char* str2,*str3;
    str2 = strtok(str,".");
    str2 = strtok(NULL,".");
    str3 = strtok(str,".");
    cout << "\n\n\tStrings: " << str2 << "," << str3;
    getch();
    }

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    I could handle it as a string, but it would be the hard solution. It can be done with a single scanf -command, but standard library can't be used in my case. I have worked with C couple of years, but now I should start using C++ commands on some occasions. For example now I'm totally puzzled with a single cin command. I've read all about cin from my 2 C++ books. I miss scanf !!!

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >but standard library can't be used in my case.
    Well, seeing as how cin is a part of the standard library you're stuck unless you feel up to writing your own input and output routines. If the input is pretty random then you'll have a hard time of trying to get formatted input, read the entire line and parse it would be my suggestion. cin is similar to scanf in it's lameness and getline is the C++ version of fgets.

    >I miss scanf
    Dear god, why?

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    559
    Prelude,

    What is it about cin that gives it its "lameness"? It's a heck of a lot easier than reading input in Java.

  7. #7
    Unregistered
    Guest
    Originally posted by Prelude
    >but standard library can't be used in my case.
    Well, seeing as how cin is a part of the standard library you're stuck unless you feel up to writing your own input and output routines. If the input is pretty random then you'll have a hard time of trying to get formatted input, read the entire line and parse it would be my suggestion. cin is similar to scanf in it's lameness and getline is the C++ version of fgets.

    >I miss scanf
    Dear god, why?

    -Prelude
    You're wrong here.

    cin is part of iostream.h
    scanf is part of stdio.h

    stdio stands for standard input/output.

    Could someone please reply to my original problem?

  8. #8
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    use cin.getline(text,15,'\n') to input

    then use isdigit in ctype.h to store all digits before '.' to different strings and then use atoi() to convert them to int.

    pretty easy

  9. #9
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    Originally posted by ihsir
    use cin.getline(text,15,'\n') to input

    then use isdigit in ctype.h to store all digits before '.' to different strings and then use atoi() to convert them to int.

    pretty easy
    Yeah, I'm doing it in hardway currently. Still I'm missing some experienced programmer to tell me if it is possible to use some syntax with cin where only 1 line is needed.

    for example input "12 34 56" can be stored to 3 variables with
    cin>>firstone>>secondone>>thirdone;

    only difference is a space replaced by dot. Please tell me if there is a simple way doing this.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    int a, b, c;
    printf("Write a random input : "); // user types 12.34.56
    scanf("%d.%d.%d", &a, &b, &c);
    printf("First parsed number %d, second parsed number %d, third parsed number %d", a, b, c);

    // ofcourse can be parsed, why not some clever reply next time?!

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    5
    Oh, a guy who said it can't be parsed removed his message.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. simple cin question
    By verbity in forum C++ Programming
    Replies: 25
    Last Post: 05-09-2007, 04:02 PM
  2. CIN Input count question?
    By kamran in forum C++ Programming
    Replies: 5
    Last Post: 10-24-2006, 05:06 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. cin Help...Sort of a noob Question?
    By Krak in forum C++ Programming
    Replies: 9
    Last Post: 01-26-2003, 01:23 PM
  5. cin question
    By joebudden in forum C++ Programming
    Replies: 5
    Last Post: 01-07-2003, 07:38 PM

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