Do/While Loops

This is a discussion on Do/While Loops within the C Programming forums, part of the General Programming Boards category; Hi, guys! First post here. I'm doing this project for the hell of it and I am improvising a lot ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    2

    Do/While Loops

    Hi, guys! First post here.

    I'm doing this project for the hell of it and I am improvising a lot of stuff to accommodate what would REALLY happen in most cases.

    So, this calculates mileage. Everything is fine until the bottom with the Do/While loops. I don't know the best way to do Y/N situations. Could I call on you to educate me on why these Do/While loops are not placed correctly? Thanks in advance!

    EDIT: I'm adding in the way I coded the heading variables just for verification.
    Code:
    	double start_mile,
    	            end_mile;
    	double reimbursement = .35;
    	char mile_yn; /* Mile Yes/No for Below */
    	char reim_yn /* Reimbursement Loop Killer */
    Code:
    int mile_calc()
    {
    	/* You Show Off */
    	printf("\nMile Reimbursement Calculator");
    	printf("\nVersion 1.0.001");
    
    	/* Starting Mileage */
    	printf("Please Enter Your Mileage Before Trip: ");
    	scanf("%.1f", &start_mile);
    
    	/* Ending Mileage */
    	printf("Please Enter Your Mileage After Trip: ");
    	scanf("%.1f", &end_mile);
    
    	/* Ask If Reimbursement Needs to be Adjusted */
    	do
    {
    	printf("Current Reimbursement: ", reimbursement);
    	printf("\nIs this correct(Y/N)?");
    	scanf("%c", &mile_yn);
    
    
    		if (mile_yn = "Y")
    		printf("Calculating...\n");
    
    		if (mile_yn = "N")
    		printf("Enter the New Reimbursement Rate: ");
    		scanf("%.2f", reimbursement);
    		printf("New Reimbursement: ", reimbursement);
    		printf("\nIs this correct(Y/N)?");
    
    		if (mile_yn = "y")
    		printf("Calculating...\n");
    
    		if (mile_yn = "n")
    		printf("Enter the New Reimbursement Rate: ");
    		scanf("%.2f", reimbursement);
    		printf("New Reimbursement: ", reimbursement);
    		printf("\nIs this correct(Y/N)?");
    		scanf("%c", &reim_yn);
    }
    		while ( reim_yn ("N"));
    
    }
    Last edited by sinamor1210; 09-19-2009 at 08:14 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    One of the first pitfalls in C is scanf().

    It works as expected for numbers, and strings, but not for single char's.

    After you take in a char with scanf(), you leave a newline char ('\n'), at the head of the keyboard buffer.

    Now when scanf() goes to look for the next single char, it see's this old newline char, and says "Great, I've found my char!", and rolls on. It will look your code is "skipping" over the scanf(), completely.

    Two fixes:

    1) add a variable = getchar() before each scanf() for a single char, or

    2) add a single space, in your scanf(),

    not this:
    Code:
    scanf("%c", &myChar);
    but this:
    Code:
    scanf(" %c", &myChar);
    Note the single extra space, before the second % sign. Makes all the difference.

    Note that I did not look at the rest of your code, after I saw the scanf() for a single char, in it. This is a *very* frequent problem for new programmers in C.

  3. #3
    Registered User
    Join Date
    Sep 2009
    Posts
    2
    Well, I fixed those references, but apparently the IF statements aren't working correctly. I'm getting tons of errors about them and about the operands used. How do I get a correct IF statement for when one of my values = Y or N?

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    With this statement,

    if (mile_yn = "Y")

    You are wanting to compare variable mile_yn (a char? a char array?) with a string constant.

    But, you aren't. Instead you are (trying to) assign ( = ) a string to the variable mile_yn. And, that's not the proper way to assign a string to a variable.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    Quote Originally Posted by sinamor1210 View Post
    Well, I fixed those references, but apparently the IF statements aren't working correctly. I'm getting tons of errors about them and about the operands used. How do I get a correct IF statement for when one of my values = Y or N?
    Code:
    if (mile_yn = "Y")
    		printf("Calculating...\n");
    'Y' is a char but this is a char array(string) "Y". So what you're doing is taking in char from the user and in the if statement assigning that char to string, which is incorrect. So the correct solution would be to compare(==) the inputted char with 'Y'(or 'N').
    Adak has well explained the other corrections in your code.
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

  6. #6
    Registered User
    Join Date
    Sep 2009
    Location
    In my skin
    Posts
    1
    Quote Originally Posted by Adak View Post
    One of the first pitfalls in C is scanf().

    It works as expected for numbers, and strings, but not for single char's.

    After you take in a char with scanf(), you leave a newline char ('\n'), at the head of the keyboard buffer.

    Now when scanf() goes to look for the next single char, it see's this old newline char, and says "Great, I've found my char!", and rolls on. It will look your code is "skipping" over the scanf(), completely.

    Two fixes:

    1) add a variable = getchar() before each scanf() for a single char, or

    2) add a single space, in your scanf(),

    not this:
    Code:
    scanf("%c", &myChar);
    but this:
    Code:
    scanf(" %c", &myChar);
    Note the single extra space, before the second % sign. Makes all the difference.

    Note that I did not look at the rest of your code, after I saw the scanf() for a single char, in it. This is a *very* frequent problem for new programmers in C.
    solved one of my other problem because of which I joined here.

    Thanks scanf was also not working for me

  7. #7
    Registered User
    Join Date
    Apr 2009
    Posts
    187
    you can after each scanf add a function which eats line for example
    while(getchar()!='\n')
    continue;
    will skip every thing after scanf like for example you type more than a answer
    like NOOO
    will only read N so it can be usefull sometimes and a single char is 'C' not "C"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 12:18 AM
  3. strings and loops...
    By twomers in forum C Programming
    Replies: 5
    Last Post: 12-12-2005, 10:28 AM
  4. Evaluation of nested loops
    By Mister C in forum C Programming
    Replies: 2
    Last Post: 08-13-2004, 01:47 PM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 03:28 PM

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