12 Replies - 866 Views - Last Post: 21 February 2012 - 11:49 PM Rate Topic: -----

#1 thathz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 12

yes/no while loop not working

Posted 21 February 2012 - 06:08 PM

This code fragment is suposed to have the user input floating point numbers into an array untill the char "yesno" is not equal to "y".

    double array[60];// Define floating point number array.
    char yesno='y'; // Define character "yessno" for while loop

    while(yesno == 'y')// start while loop for when "yesno" is equal to "y"
    {
    printf("Enter a number.\n"); //Ask user for number.
    scanf("%f", array); //assing number to array

    printf("Would you like to enter another number? (y/n) \n"); // ask user for a character if they want to input another number.
    yesno = getchar(); // asign character to "yesno" if yes no is anything but "y" loop with break.
    }



thanks for your help!

Is This A Good Question/Topic? 0
  • +

Replies To: yes/no while loop not working

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,401
  • Joined: 19-February 09

Re: yes/no while loop not working

Posted 21 February 2012 - 07:00 PM

You need to put the data into an element of the array, so use the [] brackets and a value. For a double (long float) the format is "%lf".

07	scanf("%f", array);




7.3.1.Use scanf to read data into array

This post has been edited by #define: 21 February 2012 - 07:02 PM

Was This Post Helpful? 1
  • +
  • -

#3 thathz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 12

Re: yes/no while loop not working

Posted 21 February 2012 - 08:00 PM

Ahh, opps I forgot to say what the problem I was having. The while loop donsn't break when I enter something other than "y". I'll enter "n" and the loop repeats itself.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: yes/no while loop not working

Posted 21 February 2012 - 08:10 PM

Really? Are you sure about that? I would have thought that you would have the opposite problem -- that it exits the loop immediately without waiting for you to answer the y/n question at all.

Are you sure you're testing this exact code?
Was This Post Helpful? 0
  • +
  • -

#5 thathz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 12

Re: yes/no while loop not working

Posted 21 February 2012 - 08:28 PM

View Postr.stiltskin, on 21 February 2012 - 08:10 PM, said:

Really? Are you sure about that? I would have thought that you would have the opposite problem -- that it exits the loop immediately without waiting for you to answer the y/n question at all.

Are you sure you're testing this exact code?


Yep, here's the same code I'm running:


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char yesno='y'; // Define character "yessno" for while loop
    double array[60];// Define floating point number array.
    int  i=0;// Define int for array address set in eqal to i

    printf("This program takes floating pount numbers and sorts them from lowest to highest. Press enter to continue.");

    while(yesno == 'y', getchar())// start while loop for when "yesno" is equal to "y"
    {
    printf("Enter a number.\n"); //Ask user for number.
    scanf("%f", array[i]); //assing number to array
    i++;

    printf("Would you like to enter another number? (y/n) \n"); // ask user for a character if they want to input another number.
    yesno = getchar(); // asign character to "yesno" if yes no is anything but "y" loop with break.
    }


    return 0;
}

Attached image(s)

  • Attached Image

This post has been edited by r.stiltskin: 21 February 2012 - 08:30 PM
Reason for edit:: Fixed the code tags.

Was This Post Helpful? 0
  • +
  • -

#6 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: yes/no while loop not working

Posted 21 February 2012 - 08:42 PM

Well, I know you're not running that exact code because it would crash with a segmentation fault on the first entry. You need to give scanf the address of an array element, not the value of the element, so:
scanf("%lf", &array[i])

and notice, %lf, not %f (as #define said.

Your rebellious loop is acting like that because of the bizarre structure of your control expression. This
while (yesno == 'y', getchar())
is not good. It's not using the "yesno=='y'" test at all. Instead, the getchar() is consuming whatever newline char is in the stream, this "satisfies" the while condition and the loop repeats.

Isn't your compiler giving you a warning, such as
"warning: left-hand operand of comma expression has no effect"? You shouldn't ignore compiler warnings. (If you're not getting a warning, you should make sure your IDE is configured to output warnings.)

Get rid of that getchar(), and put
getchar();
on the line after the scanf call.
Was This Post Helpful? 1
  • +
  • -

#7 thathz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 12

Re: yes/no while loop not working

Posted 21 February 2012 - 09:39 PM

View Postr.stiltskin, on 21 February 2012 - 08:42 PM, said:

Well, I know you're not running that exact code because it would crash with a segmentation fault on the first entry.


The last post was the code I was using. I copyed it and pasted it into codebocks just now to double check it ran.

Quote

is not good. It's not using the "yesno=='y'" test at all. Instead, the getchar() is consuming whatever newline char is in the stream, this "satisfies" the while condition and the loop repeats.


From my usderstanding getchar() reads the a character input from the user. By putting yesno = getchar(); after asking the user for a "n" or "y" inpet ether a "y" or "n" will be asigned to "yesno", and if anything other than y is asigned the loop should break.

Quote

Get rid of that getchar(), and put
getchar();
on the line after the scanf call.


I'm not to sure what you mean by this. I implimented what I think you ment in the following code:

    printf("Would you like to enter another number? (y/n) \n"); // ask user for a character if they want to input another number.
    scanf("%c", yesno);
    getchar();



I got a error saying "Segmentation fault" imediatly after entering a number.

Quote

Isn't your compiler giving you a warning, such as
"warning: left-hand operand of comma expression has no effect"? You shouldn't ignore compiler warnings. (If you're not getting a warning, you should make sure your IDE is configured to output warnings.)From my usderstanding getchar() reads the a character input from the user.


I don't see any errors under the "Logs & others" in Code Blocks. I looked underview and didn't see anything that looked like it would output warnings.

Thanks for edditing [/code] into my post I'm tryed looking for an edit botton, am I blind or do I have to make a certain number of posts befor being able to edit my posts?

thank you for your help!
-evan
Was This Post Helpful? 0
  • +
  • -

#8 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: yes/no while loop not working

Posted 21 February 2012 - 10:07 PM

Yes, you need a certain number (I don't know how many) posts before you can edit.

Quote

The last post was the code I was using. I copyed it and pasted it into codebocks just now to double check it ran.

Well if it didn't crash you were lucky. When I ran it it crashed immediately after I entered the first number. It crashed because in this line
scanf("%f", array[i])

you are giving scanf the value of the element in array[ i ] (which contains "garbage" because you haven't stored anything there yet). scanf needs the address where you want to store the input. So maybe your "garbage" accidentally happened to be a valid address. Mine wasn't. Anyway, that line needs to be
scanf("%lf", &array[i])





Quote

is not good. It's not using the "yesno=='y'" test at all. Instead, the getchar() is consuming whatever newline char is in the stream, this "satisfies" the while condition and the loop repeats.

There, I was talking about your while condition which was
while (yesno == 'y', getchar())

You can't have a "comma expression" (which the compiler treats as two separate expressions) inside the parentheses. The while loop condition must be a single logical expression -- it can be a "compound expression" containing ANDs && and ORs ||, but it can't have any commas or semicolons. The compiler's solution is to ignore the "yesno=='y'" part altogether, and treat the "getchar()" as your logical expression, meaning if getchar() returns a 0 it's false, and any other value is true. So you have to eliminate the getchar() from that expression and use just
while (yesno == 'y')




Quote

Get rid of that getchar(), and put
getchar();
on the line after the scanf call.


You do need a getchar() after the scanf. Why? Because your scanf is only taking a numerical input. But when you input the number, you follow it by pressing 'enter' which puts a newline character into the input stream. getchar here only wants numbers, so it stops after the last digit (or decimal point), and leaves the newline character '\n' in the stream. If you left that there, when you call getchar to get a value for yesno, it would just take the '\n' as its input and not wait for you to enter anything. So you need a getchar() call immediately after the scanf to consume that extra newline and discard it. So what you did below is correct:

Quote

I'm not to sure what you mean by this. I implimented what I think you ment in the following code:

    printf("Would you like to enter another number? (y/n) \n"); // ask user for a character if they want to input another number.
    scanf("%c", yesno);
    getchar();



Quote

I got a error saying "Segmentation fault" imediatly after entering a number.

I'm not sure what's causing that segmentation fault. If you're still getting it after making all the corrections, post the complete code at that point.


Quote

I don't see any errors under the "Logs & others" in Code Blocks. I looked underview and didn't see anything that looked like it would output warnings.

In CodeBlocks, click on Settings/Compiler and Debugger and under "compiler settings" you'll see a long list of checkboxes. Check these
enable all compiler warnings
enable extra compiler warnings
enable warnings demanded by strict ISO C and ISO C++
enable effective-c++ warnings

That will take care of your future projects, but it won't effect any projects that you've already started. For those you have to open each project, then click on Project/Build Options and that will open the equivalent set of options applicable to that particular project.

This post has been edited by r.stiltskin: 21 February 2012 - 10:09 PM

Was This Post Helpful? 1
  • +
  • -

#9 thathz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 12

Re: yes/no while loop not working

Posted 21 February 2012 - 10:33 PM

View Postr.stiltskin, on 21 February 2012 - 10:07 PM, said:

I'm not sure what's causing that segmentation fault. If you're still getting it after making all the corrections, post the complete code at that point.


Here's the current code that gives me the segmentation fault.

/*This progam takes a string of floating point numbers and sorts them from lowest to hingest, then prints the array to the console */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char yesno='y'; // Define character "yessno" for while loop
    double array[60];// Define floating point number array.
    int  i=0;// Define int for array address set in eqal to i

    printf("This program takes floating pount numbers and sorts them from lowest to highest.");

    while(yesno == 'y')// start while loop for when "yesno" is equal to "y"
    {
    printf("Enter a number.\n"); //Ask user for number.
    scanf("%lf", array[i]); //assing number to array
    i++;

    printf("Would you like to enter another number? (y/n) \n"); // ask user for a character if they want to input another number.
    scanf("%c", yesno);
    getchar();

    }

    return 0;
}



I get the segmentation fault imediatly after entering a number and hitting ender. Thanks!
Was This Post Helpful? 0
  • +
  • -

#10 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: yes/no while loop not working

Posted 21 February 2012 - 10:47 PM

You still haven't fixed line 17:

&array[ i ]

array[ i ] is the value stored in that element of the array.

&array[ i ] is the memory address where it is stored.
Was This Post Helpful? 0
  • +
  • -

#11 thathz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 12

Re: yes/no while loop not working

Posted 21 February 2012 - 10:56 PM

View Postr.stiltskin, on 21 February 2012 - 10:47 PM, said:

array[ i ] is the value stored in that element of the array.

&array[ i ] is the memory address where it is stored.


Oh gohchya

I updated the code to the following ot I get the same problem.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
    char yesno='y'; // Define character "yessno" for while loop
    double array[60];// Define floating point number array.
    int  i=0;// Define int for array address set in eqal to i

    printf("This program takes floating pount numbers and sorts them from lowest to highest.");

    while(yesno == 'y')// start while loop for when "yesno" is equal to "y"
    {
    printf("Enter a numbers.\n"); //Ask user for number.
    scanf("%lf", &array[i]); //asign number to array
    i++;

    printf("Would you like to enter another number? (y/n) \n"); // ask user for a character if they want to input another number.
    scanf("%c", yesno);
    getchar();

    }

    return 0;
}


Was This Post Helpful? 0
  • +
  • -

#12 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: yes/no while loop not working

Posted 21 February 2012 - 11:43 PM

Same thing, a little further down...

I was focusing on the scanf for the array entry and didn't notice the scanf for yesno.

scanf("%c", &yesno);

The variables you give to scanf are always addresses.
Was This Post Helpful? 0
  • +
  • -

#13 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: yes/no while loop not working

Posted 21 February 2012 - 11:49 PM

This, which is more similar to your original code, would also work

    while (yesno == 'y')
    {
        printf("Enter a numbers.\n");
        scanf("%lf", &array[i]);
        getchar(); // this is to get rid of the '\n' char that scanf leaves in the stream
        i++;

        printf("Would you like to enter another number? (y/n) \n"); // ask user for a character if they want to input another number.
        yesno = getchar();
    }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1