简体   繁体   中英

My First Program in C

Ok so I'm making a little program for twitter, basically what you do is type in how many followers you have and you will get a reply.

My problem is when you type in your amount of followers you only get the first reply for <50

Here's the code (also at http://pastie.org/1668357 ):

#include <stdio.h>

int followers;

int main() {


  {
  int followers;

  printf( "Please Enter How Many Twitter Followers You Have...\n" );
  scanf ( "%d" , &followers );
  getchar ();
}
  if ( followers > 50 ) {
   printf ("You may be new to Twitter or rarely use it, try tweeting about more topics.\n" );
   getchar ();

}

  else if ( followers < 100 ) {
   printf ("Reply: Not bad but keep Tweeting.\n" );
   getchar ();
}

  else if ( followers < 200 ) {
   printf ("Reply: People must be interested in your stuff, keep tweeting.\n" );
   getchar();
}

  else if ( followers < 500 ) {
   printf("Reply: Great, you must like Tweeting and other people must like your Tweets, Good job!\n" );
   getchar();
}

  else if ( followers < 1000 ) {
   printf ("Reply: Wow, that's a lot, send some to me @Cian_W\n" );
   getchar();
}

  else if ( followers < 1000000 ) {
   printf ("Reply: Jeez, are you famous? I'd say you like Tweeting\n" );
   getchar();
}

  else if ( followers != 1000000 ) {
   printf ("Reply: Holy crap, are you a celeb? Many people must like you.\n" );
   getchar();

}
getchar();
return 0;
}

Thanks Cian

You've typed what was apparently supposed to be: "if (followers < 50)" as "if (followers > 50)". Lines 8, 9, and 14 should also be deleted.

Edit: Since question now includes code (without line numbers), the parts that should be deleted are in the following:

{
    int followers;

    printf( "Please Enter How Many Twitter Followers You Have...\n" );
    scanf ( "%d" , &followers );
    getchar ();
}

You want to get rid of the { and } . You also want to get rid of one of the two places that followers is defined, like: int followers;

I had originally said that the one inside main should be deleted, but in reality it would probably be better to delete the global instead. It'll work either way -- the point is to assure that the followers you read from the user also gets used in the if statements. For a program this tiny, the difference between using a local and a global variable doesn't matter much, but for the long-term, it would be better to learn to avoid globals.

Remove line 9 - You have two different variables both called followers - And change line 15 to < 50

The last if should be else if ( followers >= 1000000 ) - >= not !=

You have the followers variable declared both as a global variable, and one within an inner block inside main() . When you call scanf() you are likely writing to the followers that belongs to the innermost scope. It then goes out of scope once you hit that closing brace. Try getting rid of the global followers count, and get rid of the braces around the call to scanf() . You also need to fix the logic in your first if statement so the comparison goes the other way.

You should check form opposite diretion.

if( x > 1000000)
{
}
else if(x > 10000)
{
}
else if(x > 50 )
{
}

Or you should match exact interval

if(x<50)
{
}
else if((x>=50) && (x<1000))
{
}
else if((x>=1000) && (x<2000))
{
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM