473,320 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

scanf is resetting the value???

I have a code which is implementing a stack using array..
the code is

[vihang@nine tmp]$ nl pb4.c
1 #include <stdio.h>
2 char stack[100];
3 void push(char i,int top)
4 {
5 stack[top]=i;
6 }
7 void pop(int top)
8 {
9 printf("\n %c",stack[top]);
10 }
11 int main()
12 {
13 int t=0,l;
14 char a,b='y';
15 while(b=='y')
16 {
17 printf("\n\tenter a character\t");
18 scanf("%s",&a);
19 printf("\t1st %d\n",t);
20 push(a,t);
21 t++;
22 printf("\t2nd %d\n",t);
23 if(t>=100)
24 {
25 break;
26 }
27 else
28 {
29 printf("\n do you want to enter another character
y/n\t");
30 scanf("%s",&b);
31 }
32 }
33 l=t-1;
34 while(l>=0)
35 {
36 pop(l);
37 l=l-1;
38 }
39 return(0);
40 }
41

the out put is

[vihang@nine tmp]$ ./a.out

enter a character 1
1st 0
2nd 1

do you want to enter another character y/n y

enter a character 2
1st 0
2nd 1

do you want to enter another character y/n n

if you see the line 21 i am incrementing the value of t,
so if you look at the line 22 it show the incremented value..
but when i try to print it again (line 19) it is showing
non incremented value.

What may be the problem??

thanks in adavance

Nov 15 '05 #1
2 1340
vihang wrote:
I have a code which is implementing a stack using array..
the code is

[vihang@nine tmp]$ nl pb4.c
1 #include <stdio.h>
2 char stack[100];
3 void push(char i,int top)
4 {
5 stack[top]=i;
6 }
7 void pop(int top)
8 {
9 printf("\n %c",stack[top]);
10 }
11 int main()
12 {
13 int t=0,l;
14 char a,b='y';
15 while(b=='y')
16 {
17 printf("\n\tenter a character\t");
18 scanf("%s",&a);

%s stores input as a string. You can't store any interresting string in
a char.
The code here risks overflowing, and Real Bad things here.
Also remember to check the return value of scanf, and expect
illegal user input. Remember to read any \n that a user probably types
as he hits enter.
Nov 15 '05 #2

Nils O. Selåsdal wrote:
Remember to read any \n that a user probably types
as he hits enter.


How to read these \ns ???

Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Intaek LIM | last post by:
Hello. I always used cin in C++ for standard input. But, with C, I must use scanf. Integers, chars are all fine with scanf. The problem is... I cannot read floating point value like 'double'...
7
by: hugo27 | last post by:
obrhy8 June 18, 2004 Most compilers define EOF as -1. I'm just putting my toes in the water with a student's model named Miracle C. The ..h documentation of this compiler does state that when...
68
by: stasgold | last post by:
Hello. I maybe reinvent the weel ... I'm trying to read positive integer number with the help of scanf, if the input value is not positive number but negaive one zero or char , i have to reread...
9
by: Cao Yi | last post by:
Hi, here's a fract of codes, and what's the line "scanf("%lf%*", &cvi)" doing? ============================= do { printf("\nCoefficient: "); scanf("%lf%*", &cvi); getchar(); } while (cvi <=...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.