NO

Author Topic: characters  (Read 5892 times)

sannemander

  • Guest
characters
« on: November 22, 2011, 02:25:09 PM »
Hello again!

got another question about a code, it´s this part:

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




int main(){



float fArray[30] = {0};
int x = 0;
char cResponse = '\0';
      
   
   do{
         
      
        system ("cls");
        printf("Enter GPA: ");
          scanf("%f", &fArray
  • );

        x++;
          printf("Enter another GPA (Y/N?): ");
        scanf("%c", &cResponse);
         
         
   }while (cResponse == 'Y' || 'y');

Somehow when i run it i cannot enter y or Y, it just skips this part, maybe i miss a include file or  the array counts on to 30 or something.
A little tip would be useful .

thx!

CommonTater

  • Guest
Re: characters
« Reply #1 on: November 22, 2011, 04:46:25 PM »
This part...
Code: [Select]
    system ("cls");
        printf("Enter GPA: ");
          scanf("%f", &fArray
■);


Is pretty obviously messed up...  first it's a syntax error (which may be from copying the code here... but .... second you need to specify an array index...
Code: [Select]
scanf("%f",&fArray[x]);

Second, I'm guessing they didn't explain how scanf() works in your course... so you should know that most input functions leave the <enter> key in the keyboard input buffer (stdin) so that when the next function only wants to read a single character it finds the enter key and goes whizzing right past.  In the case of scanf() there's a little trick you can use to deal with this...
Code: [Select]
        scanf(" %c", &cResponse);
... note the space between the " and % in the formatting string.  This tells scanf() to skip over invisible characters and wait for new data. 





« Last Edit: November 22, 2011, 04:51:33 PM by CommonTater »

sannemander

  • Guest
Re: characters
« Reply #2 on: November 22, 2011, 05:46:31 PM »
hello

I am not doing a course, i try to learn it myself and want to start a course next year, that´s why they didn´t explained me...
I did not copy the code, but maybe some people use the same book than me and tend to make  the same mistakes ;)

thx for the help common tater, you are always very quick!!!

the last code came out fine!!!

sannemander

  • Guest
Re: characters
« Reply #3 on: November 22, 2011, 05:48:20 PM »
ah and the first mistake is a copying mistake, in the code it was fine ;D

CommonTater

  • Guest
Re: characters
« Reply #4 on: November 22, 2011, 06:02:59 PM »
hello

I am not doing a course, i try to learn it myself and want to start a course next year, that´s why they didn´t explained me...
I did not copy the code, but maybe some people use the same book than me and tend to make  the same mistakes ;)

thx for the help common tater, you are always very quick!!!

the last code came out fine!!!

I was referring to copying it into the forum from your IDE... 

If you're not on a course, I trust you have some kind of tutorial or textbook to work from.  C isn't a complex language, but it has a lot of quirks that need to be understood to produce successful programs.

As they say... C is easy. Programming is hard.

sannemander

  • Guest
Re: characters
« Reply #5 on: November 22, 2011, 08:09:52 PM »
I´m using the book c programming for the absolute beginner by michael vine.

Got one more question though, look at this part:

   system("cls");
   printf ("Enter GPA: ");
   scanf ("%f", &iArray
  • ,x++);

    printf("enter another GPA(Y//N?): ");
   scanf(" %c", &cResponse);
   
      }while ((x > 30)||(cResponse == 'y' || 'Y'));

Whatever letter i put, it starts the loop again until 30 times, a n or N or whatever doesn´t stop it, what am i missing?

CommonTater

  • Guest
Re: characters
« Reply #6 on: November 22, 2011, 08:52:11 PM »
Before you do anything else...
fix this as I explained above.
Code: [Select]
   scanf ("%f", &iArray
■,x++);


should be...
Code: [Select]
scanf("%f",&iArray[x]);
x++;

It won't even compile as it is...

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: characters
« Reply #7 on: November 22, 2011, 09:09:04 PM »
I´m using the book c programming for the absolute beginner by michael vine.

Got one more question though, look at this part:
Code: [Select]
system("cls");
printf ("Enter GPA: ");
scanf ("%f", &iArray[x],x++);
    printf("enter another GPA(Y//N?): ");
scanf(" %c", &cResponse);

      }while ((x > 30)||(cResponse == 'y' || 'Y'));

Whatever letter i put, it starts the loop again until 30 times, a n or N or whatever doesn´t stop it, what am i missing?
Well, for one, there is this problem with the copy&paste oft he code here, you should put "code" tags around that part. (it's the "hash" (#) button above the forums entry window!)

Beside that, the most obvious problem I see is that you are missing to initialize the variable x in this code snippet, which could lead to all kinds of strange side effects, but then this doesn't seem to be a complete snippet anyway, as there is no start of the while statement at least. This all makes it so much more difficult to help you...

General logic programming issues otherwise are:
- the condition of the while statement "(x > 30)" isn't likely to be true from the start, very suspicious at least without knowing how you initialized it. But then you are incrementing x, so you at least intend likely to start at zero.
- the increment x part is also never executed, as you included that into the scanf ("%f...") statement, which makes me wonder why the compiler doesn't complain about that
- further more, the second condition of the while loop isn't right, as "(cResponse == 'y' || 'Y')" will never be true, you are likely to intend to use "((cResponse == 'y') || (cResponse ==  'Y'))"


Ralf

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: characters
« Reply #8 on: November 22, 2011, 09:12:22 PM »
Before you do anything else...
fix this as I explained above.
Code: [Select]
   scanf ("%f", &iArray
■,x++);


should be...
Code: [Select]
scanf("%f",&iArray[x]);
x++;

It won't even compile as it is...
That's a "bug" of the forum software, it looks ok if you put it into code tags, otherwise the forum software is interpreting the "[ x ]" as a BB codetag :


Ralf

CommonTater

  • Guest
Re: characters
« Reply #9 on: November 22, 2011, 09:45:20 PM »
That's a "bug" of the forum software, it looks ok if you put it into code tags, otherwise the forum software is interpreting the "[ x ]" as a BB codetag :

Ralf

Thank you Ralf... noted for future....


Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: characters
« Reply #10 on: November 22, 2011, 10:38:30 PM »
As you are creating a console program, to might be better to use actually functions from conio.h instead of the somewhat "costly" system() call.

Just this part of what might be a larger program would look as a working standalone version like this:
Code: [Select]
#include <stdio.h>
#include <conio.h>

int main (void)
{
int x;
float iArray [31];
char cResponse;

x = 0;

do
{
          _clrscr();
printf ("Enter GPA: ");
  scanf ("%f", &iArray[x++]);

printf("enter another GPA(Y/N?): ");
  scanf(" %c", &cResponse);

     } while ( (x < 30)  &&  ((cResponse == 'y') || (cResponse ==  'Y')) )  ;
    return 0;
}
Ralf
« Last Edit: November 22, 2011, 10:47:12 PM by Bitbeisser »

sannemander

  • Guest
Re: characters
« Reply #11 on: November 23, 2011, 12:21:59 PM »
thx for the help!

what is actually the difference between conio.h and system() call?
for the rest, my exercise tells me to make an array of 30, as far as I know this array goes from 0 to 29, that´s why I put x<30(x is smaller than 30, so 29 maximum) but correct me if i´m wrong!
Next time I look for the hash button ;D

Sanne



CommonTater

  • Guest
Re: characters
« Reply #12 on: November 23, 2011, 06:28:36 PM »
thx for the help!

what is actually the difference between conio.h and system() call?

The conio.h method does not have to launch an external program whereas the system() method does.  Ironically it's very likely the external program probably just calls the internal function anyway. 

Quote
for the rest, my exercise tells me to make an array of 30, as far as I know this array goes from 0 to 29, that´s why I put x<30(x is smaller than 30, so 29 maximum) but correct me if i´m wrong!

Nope, you've got that figure out just fine... Many people mistake "the number of elements" for "the number of the element" and end up with buffer overflows on the last element of their arrays.  For ... char array[30]; ... you have elements numbered from 0 to 29 so the ... X < 30 ... approach is good.

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: characters
« Reply #13 on: November 23, 2011, 11:38:05 PM »
thx for the help!

what is actually the difference between conio.h and system() call?
The conio.h calls are direct calls to the respective OS function/interrupt while the system () call will call an external program/command, in this case it will execute the "CLS" command of the command shell interpreter. This is more "costly" as it needs to do a lot of things that are simply not necessary for a direct function call....
Quote
for the rest, my exercise tells me to make an array of 30, as far as I know this array goes from 0 to 29, that´s why I put x<30(x is smaller than 30, so 29 maximum) but correct me if i´m wrong!
Well, the major problem in your original code was that you wrote "X >30, which by the program logic simply wasn't right. This would have been "false" from the first pass and would have led to an immediate termination of the loop, beside the fact that the check on the y/n question wasn't working as you wrote it either...
Quote
Next time I look for the hash button ;D
It's right above the post edit area, slightly right and above the row of smiley's    ;)

Ralf