Damn stack!!

This is a discussion on Damn stack!! within the C++ Programming forums, part of the General Programming Boards category; I wrote up a little console program and the darn thing don't work properly. It scans for a keybord input ...

  1. #1
    Unregistered
    Guest

    Angry Damn stack!!

    I wrote up a little console program and the darn thing don't work properly.
    It scans for a keybord input using scanf. Only, when I enter a proper value and press enter, it does not go to the next step. I have to input another value and press enter again. This time it works but the second value I inputed is passed to the second question. The value I entered for the second question is passed to the third question and so on.

    Really annoying.

    I'm sure somebody can help me?

  2. #2
    Peace
    Join Date
    Aug 2001
    Posts
    1,510
    code?
    "There's always another way"
    -lightatdawn (lightatdawn.cprogramming.com)

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    scanf does that because it leaves stuff in the input buffer, when you go for the next read everything in the buffer will be read instead of what you type. Use getchar to get rid of that.
    Code:
    int main(){
    	int x, y;
    	printf("read one");
    	scanf("%d", &x);
    	getchar();
    	printf("read two");
    	scanf("%d", &y);
    
    	printf("%d %d", x, y);
    	return 0;
    }
    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    LOL Prelude it's that scanf function again.
    at the risk of sounding boring I'm gonna say it again NEVER use scanf, for the reason Prelude gave.
    Theres always an alternative which may mean extra coding but will be far safer.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    There's always an alternative to scanf, it's always better, but schools only teach scanf for some stupid reason :P

    -Prelude
    My best code is written with the delete key.

  6. #6
    Unregistered
    Guest

    Arrow Furthermore...

    Here is the code I wrote :

    printf("\n\nEnter the Young's longitudinal modulus for the fibers in GPa :\n");
    scanf(" %f ", &e1f);
    printf("\nYoung's longitudinal modulus for the fibers is %4.2f GPa.\n", e1f);
    printf("\nEnter the Young's transverse modulus for the fibers in GPa :\n");
    scanf(" %f ", &etf);
    printf("\nYoung's tranverse modulus for the fibers is %4.2f GPa.\n", etf);
    printf("\nEnter the shear modulus for the fibers in GPa :\n");
    scanf(" %f ", &g12f);
    printf("\nShear modulus for the fibers is %4.2f GPa.\n", g12f);
    printf("\nEnter the major Poisson's ration for the fibers :\n");
    scanf(" %f ", &poissonf);
    printf("\nMajor Poisson's ration for the fibers is %.3f.\n", poissonf);
    printf("\nEnter the Young's modulus for the matrix in GPa :\n");
    scanf(" %f ", &em);
    printf("\nYoung's modulus for the matrix is %4.2f GPa.\n", em);
    printf("\nEnter the Poisson's ration for the matrix :\n");
    scanf(" %f ", &poissonm);
    printf("\nMajor Poisson's ration for the matrix is %.3f.\n", poissonm);
    printf("\nEnter the fiber volume fraction to be used for all of the plies :\n");
    scanf(" %f ", &fibvol);
    printf("\nThe fiber volume ratio for the laminate is %4.3f.\n", fibvol);

    I tried getchar() after scanf as you suggested Prelude. It works only now I have to press [enter], a number and [enter] for each question. It seems as though scanf cannot detect the end of input on the first [enter].

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Yet another reason to not use scanf. Look up functions such as fgets, getchar, and fgetc. Those are good replacements.

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest

    Thumbs up

    Thanks Prelude.

    I used fgets with string conversion functions and now my code works like a charm.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack and pointer problem
    By ramaadhitia in forum C Programming
    Replies: 2
    Last Post: 09-12-2006, 12:41 AM
  2. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  3. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Stack Program Here
    By Troll_King in forum C Programming
    Replies: 7
    Last Post: 10-15-2001, 06:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21