11 Replies - 609 Views - Last Post: 08 February 2010 - 10:48 AM Rate Topic: -----

#1 Metropoler  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 29-December 09

Cin is making trouble

Posted 07 February 2010 - 12:47 PM

Hello together,

I am creating Tic Tac Toe at the moment and I want to read an input like "1,2". This input is saved in a char array.
To simplify my question I cut out a lot of things and I got back to the root problem.

Here is the code:
void humanTurn() {
    int x = 0, y = 0;
    char input[20];

    size_t divider = 0;
    cout << "Where do you would like to set your sign? ";
    cin.get(); //to attempt the enter key
    cin.getline(input, 20);


    
    //2C = ','
    divider = ((string)input).find_first_of(0x2C);
    x = atoi((((string)input).substr(0, (int)divider)).c_str());
    y = atoi((((string)input).substr((int)divider+1, ((string)input).length())).c_str());

    humanTurn();
}


This is the basic code. Now I will show you and example run:

Quote

Where do you would like to set your sign?


Is This A Good Question/Topic? 0
  • +

Replies To: Cin is making trouble

#2 Metropoler  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 29-December 09

Re: Cin is making trouble

Posted 07 February 2010 - 12:54 PM

Hello together,

I am creating Tic Tac Toe at the moment and I want to read an input like "1,2". This input is saved in a char array.
To simplify my question I cut out a lot of things and I got back to the root problem.

Here is the code:
void humanTurn() {
    int x = 0, y = 0;
    char input[20];

    size_t divider = 0;
    cout << "Where do you would like to set your sign? ";
    cin.get(); //to attempt the enter key
    cin.getline(input, 20);
    cout << "\nInput: " << input[0] << input[1];

    //this part will cut the x and y coord out of the char array.
    //2C = ','
    divider = ((string)input).find_first_of(0x2C);
    x = atoi((((string)input).substr(0, (int)divider)).c_str());
    y = atoi((((string)input).substr((int)divider+1, ((string)input).length())).c_str());

    humanTurn();
}


This is the basic code. Now I will show you and example run:

Quote

Where do you would like to set your sign? 1,2
Input: 1,
Where do you would like to set your sign? 1,2
Input: ,2


I don't know where the mistake is, but the program starts to cut the first char off.
The most interesting part is, that after the first input round it will ALWAYS cut the first char off - and just the first not any more.

I tried to reset the array with the memset function every round or with a for loop to enter in every position a zero, but this didn't help either.

I would be delighted if you could help to get this fixed.
Was This Post Helpful? 0
  • +
  • -

#3 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: Cin is making trouble

Posted 07 February 2010 - 12:57 PM

getline(cin,stringvariablename); // gets a line, not like using get or >> which doesn't include whitespaces

edit: woops you're using a character array..
cin.getline(arrayname,lengthofarray); // this works on my compiler (visual studio 2008 express)



make sure cin is still good
if(cin)
{
//it's ok
}


you can get input as:
while(cin)
{
//cin.getline();
}


This post has been edited by taylorc8: 07 February 2010 - 01:03 PM

Was This Post Helpful? 1
  • +
  • -

#4 Guest_Sam*


Reputation:

Re: Cin is making trouble

Posted 07 February 2010 - 01:00 PM

I'm not sure what your problem is because you didn't really ask a question, but I am curious why your input[] array is of length 20? The most anyone should input is 3 characters, '1,2', '2,3', etc. So your input array should just be input[3]. You'll have to error check on the length of the input to make sure you don't go out of bounds.
Was This Post Helpful? 0

#5 Metropoler  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 29-December 09

Re: Cin is making trouble

Posted 07 February 2010 - 01:32 PM

The while-loop doens't help at all, because I can't really get on in that program with it, or I might just don't know it.

Well the if clause doesn't help me either, because I can normally enter an input like before but nothing has changed. In the second round the first char is going to be cut off.


View Posttaylorc8, on 07 February 2010 - 11:57 AM, said:

getline(cin,stringvariablename); // gets a line, not like using get or >> which doesn't include whitespaces

edit: woops you're using a character array..
cin.getline(arrayname,lengthofarray); // this works on my compiler (visual studio 2008 express)


I know that getline gets a whole line, that's the reason I am using it.
Your cin.getline definition fits to mine. The arrayname is 'input' and length is '20'.

cin.getline(input, 20)



maybe somebody else knows what to do?

This post has been edited by Metropoler: 07 February 2010 - 01:33 PM

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Cin is making trouble

Posted 07 February 2010 - 01:47 PM

your input isn't set to the first two indexes. getline() fills the array from the entire line.
Was This Post Helpful? 0
  • +
  • -

#7 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Cin is making trouble

Posted 07 February 2010 - 01:55 PM

This is a duplicate thread to this one:
http://www.dreaminco...topic154512.htm

This post has been edited by janotte: 07 February 2010 - 01:55 PM

Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Cin is making trouble

Posted 07 February 2010 - 01:59 PM

Thanks for pointing that out.


*Threads merged* Please do not create duplicate threads on the same topic.
Was This Post Helpful? 0
  • +
  • -

#9 Metropoler  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 29-December 09

Re: Cin is making trouble

Posted 07 February 2010 - 04:07 PM

View PostSam, on 07 February 2010 - 12:00 PM, said:

I'm not sure what your problem is because you didn't really ask a question, but I am curious why your input[] array is of length 20? The most anyone should input is 3 characters, '1,2', '2,3', etc. So your input array should just be input[3]. You'll have to error check on the length of the input to make sure you don't go out of bounds.


I don't think that's a big deal :-) Yes, you can set it to 3. But I have an input problem.

View PostKYA, on 07 February 2010 - 12:47 PM, said:

your input isn't set to the first two indexes. getline() fills the array from the entire line.


Yes, that's the way I want to have it.

Maybe my english is quite poor and that's the reason why you haven't understood me yet.

1. I have to enter coordinates like '1,2' or '2,2'.
2. I have insert a cout which gives out the input which I have entered.
3. Then the whole methods starts again.

Ok and now I will give you an example:

1. Input: 1,2
2. cout: 1,2
3. Goto 1.
1. Input: 2,2
2. cout: ,2
3. Goto 1.
1. Input: 1,3
2. cout: ,3
3. Goto 1.
1. Input: 2,1
2. cout: ,1
...

I have highlighted the outputs. I haven't forgot the number before the comma, that's the issue here. After the second run the first char is cut off. I don't know why.
How to fix this?
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: Cin is making trouble

Posted 07 February 2010 - 08:13 PM

The problem here is that you expect perfect (or close) input from the user which isn't the best case to go. You only need 'num,num'? Then make the array have 3 slots. Then check the first and last index for your numbers.

Alternative, strtok() around the comma to get the individual values, it's up to you on how exactly you want to approach it.



the real question is, why are you using C style strings with a C++ program?
Was This Post Helpful? 0
  • +
  • -

#11 Guest_kajiramre*


Reputation:

Re: Cin is making trouble

Posted 08 February 2010 - 06:41 AM

I'm pretty clueless, but maybe what your problem is, is that there are left over characters in the array where you are trying to get your numbers from. I know I need to put a cin.ignore() statement in my programs to remove the space that was left over from the previous input. Hope that helps.
Was This Post Helpful? 0

#12 Metropoler  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 78
  • Joined: 29-December 09

Re: Cin is making trouble

Posted 08 February 2010 - 10:48 AM

View PostKYA, on 07 February 2010 - 07:13 PM, said:

You only need 'num,num'?

Yes you are right. But I wanted to use this method to argue with C++ and learn new functions.



View PostKYA, on 07 February 2010 - 07:13 PM, said:

the real question is, why are you using C style strings with a C++ program?

What's C style here? Maybe you mean the chararray? I need it for getline().


View Postkajiramre, on 08 February 2010 - 05:41 AM, said:

I know I need to put a cin.ignore() statement in my programs to remove the space that was left over from the previous input. Hope that helps.

Good to know this command, but in my case it removes the 2nd char, too. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1