scanf problem

J

Jack

Hi, can someone tell me what is wrong with this code.

cout << "Enter the size of the array:";
cin >> size;

myVar = new char[size];
myVar[0] = 'a';

printf ("%c\n",myVar[0]);
puts ("Enter value for the first cell: ");
scanf("%c", myVar[0]);
printf ("%c",myVar[0]);

The problem occures at scanf() and I get a segmentation fault error.
 
A

Adrian Hawryluk

Jack said:
Hi, can someone tell me what is wrong with this code.

cout << "Enter the size of the array:";
cin >> size;

myVar = new char[size];
myVar[0] = 'a';

printf ("%c\n",myVar[0]);
puts ("Enter value for the first cell: ");
scanf("%c", myVar[0]);
printf ("%c",myVar[0]);

The problem occures at scanf() and I get a segmentation fault error.
The parameters you pass to scanf for loading in values have to be
addresses. I.e.
scanf("%c", &myVar[0]);
or
scanf("%c", myVar);

Also, if you are using both streams and stdio, you should use the function:

bool sync_with_stdio( bool sync=true );

Or you could have problems.

Hope this helps.



Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_http://adrians-musings.blogspot.com/]______\/
 
I

int2str

Hi, can someone tell me what is wrong with this code.

It doesn't compile, for once. Please post a compilable example that
shows your problem and describe your problem a little better than just
asking "what's wrong".
cout << "Enter the size of the array:";
cin >> size;

Looks like we're writing C++ here...
myVar = new char[size];
myVar[0] = 'a';

printf ("%c\n",myVar[0]);

Now we seem to be in C land...
puts ("Enter value for the first cell: ");
scanf("%c", myVar[0]);

scanf() takes a pointer as it's second parameter. So either use myVar
alone, or &myVar[0] here.
printf ("%c",myVar[0]);

The problem occures at scanf() and I get a segmentation fault error.

Cheers,
Andre
 
J

Jack

Thanks for the reply. So I changed the code to the following:

int main(){
char * myVar;
int size;
printf ("Enter the size of the array: ");
scanf("%d", &size);

myVar = new char[size];
myVar[0] = 'a';

printf ("%c\n",myVar[0]);
printf ("Enter value for the first cell: ");
scanf("%c", &myVar[0]);
printf ("%c",myVar[0]);
return 0;
}

The code still doesn't work since it doesn't stop to get the input
from user on the second scanf() function. But if I change the %c in
scanf("%c", &myVar[0]); to %s, it works fine. Any idea why this is?



Hi, can someone tell me what is wrong with this code.

It doesn't compile, for once. Please post a compilable example that
shows your problem and describe your problem a little better than just
asking "what's wrong".
cout << "Enter the size of the array:";
cin >> size;

Looks like we're writing C++ here...
myVar = new char[size];
myVar[0] = 'a';
printf ("%c\n",myVar[0]);

Now we seem to be in C land...
puts ("Enter value for the first cell: ");
scanf("%c", myVar[0]);

scanf() takes a pointer as it's second parameter. So either use myVar
alone, or &myVar[0] here.
printf ("%c",myVar[0]);
The problem occures at scanf() and I get a segmentation fault error.

Cheers,
Andre
 
A

Adrian Hawryluk

Jack said:
Thanks for the reply. So I changed the code to the following:

int main(){
char * myVar;
int size;
printf ("Enter the size of the array: ");
scanf("%d", &size);

myVar = new char[size];
myVar[0] = 'a';

printf ("%c\n",myVar[0]);
printf ("Enter value for the first cell: ");
scanf("%c", &myVar[0]);
printf ("%c",myVar[0]);
return 0;
}

The code still doesn't work since it doesn't stop to get the input
from user on the second scanf() function. But if I change the %c in
scanf("%c", &myVar[0]); to %s, it works fine. Any idea why this is?

Don't top post and quote unnecessarily please.

You should also do some error checking. See "scanf problem" posted in
this newsgroup for more info on that.

The reason for it not working is probably because (IIRC) the whitespace
isn't processed after the last scanf, so your "%c" reads in that
whitespace (probably a \n), "%s" skips whitespace till it gets to a
non-whitespace char and reads in from there. If you want that behavior,
use "%1s".

Hope this helps.


Adrian
--
_____________________________________________________________________
\/Adrian_Hawryluk BSc. - Specialties: UML, OOPD, Real-Time Systems\/
\ My newsgroup writings are licensed under a Creative Commons /
\ Attribution-Share Alike 3.0 License /
\_______[http://creativecommons.org/licenses/by-sa/3.0/]______/
\/_______[blog:_http://adrians-musings.blogspot.com/]______\/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top