Click here to Skip to main content
15,908,673 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Facing Problem in cin && cin.get in 2D CString
i want to input a single character not a whole line, if the user enters more then one character, if would not accept, terminate, crash etc .. but still unable to do sO.!!
now if i use 1D array and use cin,, it will getting the the characters more than one,, weird output but if i use 2D as i know the there are 5 c arrays 1 is used for length of the string, then the cin.get is as : cin.get(dummy.name[i], 2) i'm passing 2 here one for character and +1 for null character.
but still the output is not correct. . .
if pass 1 instead 2 .. the problem is still there ..


C++
#include <iostream>
#include <cstring>
using namespace std;


struct Student
{
    char name[5][1];
};

void in(Student &dummy)
{
    for(int i = 0; i < 5; ++i)
    {
       // cin >> dummy.name[i]; 
       cin.get(dummy.name[i], 1);
    }
}

void out(Student &dummy)
{
    for(int j = 0; j < 5; ++j)
    {
        cout << dummy.name[j] << " || ";
    }
}

int main()
{
    Student nerd;
    in(nerd);
    cout << '\n';
    out(nerd);
    return 0;
}
Posted
Comments
CPallini 27-Aug-13 6:54am    
Since you are programming in C++ language, why don't you use std::string for maintaining the student name?
Usman Hunjra 27-Aug-13 7:14am    
yes, i've written this code in last semester with string .. but CString ??
Thats' just for the sake of knowledge ..

Your name[i] is of type char*, not char, so your code is trying to read and write entire strings. Since you didn't allocate memory for these strings, this will cause undefined behaviour.

cin may not be the best tool to read input character by character. You may be better off using C functions such as getchar()[^]. But if you must use cin, be advised that every input, even a single character, must be followed by [return] before cin can process it. In this case please check this tutorial on basic io[^]
 
Share this answer
 
Comments
CPallini 27-Aug-13 6:51am    
5.
Usman Hunjra 27-Aug-13 7:06am    
hmm pretty good .. ;) +5
please guide me more ..
you talk about memory allocation ,, you mean the new operator that we use when we have a pointer to character .. ??
but at this time i want to stick with array .. i know the arrays are actually pointers . ??
Stefan_Lang 27-Aug-13 7:41am    
First of all, what I meant is that your variable declaration is incorrect: you defined a fixed size 2D array for at most 5*1=5 *characters*, but you're trying to store up to 5 *strings* in there. It's not so much that you failed to allocate memory, but that your structure is insufficient to hold the data.

I suspect however that you didn't actually mean to store 5 strings in there - therefore you should consider what kind of data that variable 'name' should hold, and then declare it correctly. Hint: in C++ the correct type to hold a single string is std::string .
Usman Hunjra 27-Aug-13 11:36am    
i don't know how do that ..
if you please give me an example i'll be very thankful to you ..
yeah i got your point i.e string name[5] .. i think this works like a charm ..
but i want to store just a single character there .. !!!!!!
If you want to use the form
C++
get (char* s, streamsize n)

the get function tries to read n-1 characters from the stream and then appends a null-terminator. If you use n==1 then nothing can be read. If you want to use n==2 then you must provide space for the null-terminator, which you would do by:
C++
char name[5][2];


Alternatively you could use the get function that reads a single character:
C++
cin.get(dummy.name[i][1]);

In that case your out function would not work, because it also is written in a way so that it expects null-terminated strings. Instead of
C++
cout << dummy.name[j] << " || ";

you need to do
C++
cout << dummy.name[j][1] << " || ";

The reason is that dummy.name[j] specifies an object of type char[] (or char*) in which case the << operator expects a null-terminated c-string. dummy.name[j][1] however is a single character and the << operator knows how to deal with that and does not expect a null-terminator behind it.

Hope that helps.
 
Share this answer
 
Comments
Usman Hunjra 27-Aug-13 14:29pm    
thank you .. :)
code updated but still not working >> after getting three characters the program terminates .. :(
nv3 27-Aug-13 15:30pm    
Strange. Of course you could use a debugger to find out what is going on. Making yourself familiar with a debugger is probably the best investment in your programming career.

Just a quick question. How are you entering the characters? Like
>ABCDE
or
>A B C D E
The latter would explain the behaviour you describe, because the program also reads the spaces as single characters and puts them into your array. Then, after the C you have read 5 characters and the program continues with the output. Is that what is happening here? Perhaps you should read another character at the end of main so that your program stays in a prompt and you have time enough to inspect your output.
Usman Hunjra 27-Aug-13 15:35pm    
i appreciate that you there ..
i input like that > a then press enter > b enter > c hit enter and the output is weird ..
nv3 27-Aug-13 15:40pm    
then try
>ABCDE
because your program expect single uninterrupted characters. Then tell me what the output is, please.
Usman Hunjra 27-Aug-13 15:47pm    
YES, IT WORKS .. !!
but i don't want in that way ..
Sir Please I need your help here ..
i just want to input a single character that's why i'm using char .!! i interested in storing the first character. just !!!! ??
what i know is: char name[5][2] >> means that i can store 5 strings with maximum length of 1 character +1 for null character .. !!
Presuming C# here, so ... in your Input Window, an edit control, have you tried using an 'input mask'? It only allows what you want to see.

Also, don't forget in the array [] the last slot is for size. So char name[5][1] will get you name[0-4][0], so cin.get(dummy.name[i][0], 1); will do the trick.
 
Share this answer
 
Comments
Richard C Bishop 27-Aug-13 14:50pm    
The OP is speaking C++, your answer is off-topic.
The_Inventor 27-Aug-13 23:03pm    
The thing I was trying to point out was more clearly stated in Solution 3.
u p d a t e d .. !!

C++
#include <iostream>
#include <cstring>

using namespace std;


struct Student
{
    char name[5][2];
};

void in(Student &dummy)
{
    for(int i = 0; i < 5; ++i)
    {
       cin.get(dummy.name[i][1]);
    }
}

void out(Student &dummy)
{
    for(int j = 0; j < 5; ++j)
    {
        cout << dummy.name[j][1] << " || ";
    }
}

int main()
{
    Student nerd;
    in(nerd);
    cout << '\n';
    out(nerd);
    return 0;
}
</cstring></iostream>
 
Share this answer
 
Comments
pasztorpisti 27-Aug-13 16:02pm    
Please don't post messages as answers!
Usman Hunjra 27-Aug-13 17:27pm    
i apologize .. !!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900