-
Ok, i have a little problem.
I have a 128 sized character array
chararray[128]
and im trying to have the user input stored in the array
cin >> chararray;
but it gives me an illegal operation when the user hits enter.
Now, i havent learned pointers yet but im traying to make a constant array that dosent resize, but only stores values.
This is a basic exercise im trying to work on just to see if it works. Can anyone help me out? :)
Thanks :D
-
Have you tried using cin.getline? That will make it slightly more flexible when using an array.
-
okay thanks i'm gonna go try that right now.
:)
-
actually, i just tried it and i still get the illegal operation.
Code:
char fractarray[128]
cin.getline(fractarray, 128, '\n');
any other suggestions?
thanks
-
This works fine for me:
Code:
#include <iostream.h>
void main() {
char fractarray[128];
cin.getline(fractarray, 128, '\n');
fractarray[127] = 0;
cout << fractarray << endl;
}
The main change I made was to make sure that it is always null-terminated by setting the last character to 0.
-
Ok, this is stupid. I dont understand why its not working. It will give me a illigal operation but then it will cout the output. So I dont get it. I kind of feel stupid for not knowing how to do this since it seems so simple? ... I did exactly what you did, do you think there might be something wrong with my C version?
Im using VC++ 6 btw.
If you want I can post more code on there, but since its that chunk of code which isnt working, I thought it was the only problem... everything before and after works good if i take the cin out.
Any other suggestions?
Thanks for the help! :)
-
Bizarre :confused:
On VC++5 (no SP) it worked perfectly...I severely doubt it's a problem with VC++6, though. What code do you have that produces the illegal operation? Maybe something else has confused it somewhere.
-
Ok wierd, I went to another machine and I did the same code that you posted and it worked fine!!!
Now, I went back and I clicked on rebuild ALL and it executed fine.
So its fixed, I just dont know how?
O well, thanks for all the replies :)
-
Hmm :) Rebuild All does tend to fix things in many cases :rolleyes:
Anyway, after a few more questions I would have had to have given up anyway...my brain's totally fried (5am here 'cause I'm still working on this $"&"&$ coursework!)