#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0

    New to C. Need assistance to my first school exercise.


    Hi, I'm using dev c ++ and i'm getting some errors on this project. Bad thing is that i can't post a picture because i'm a new user.

    #include <stdio.h>
    #include <stdlib.h>
    { main ()
    float B_M;
    printF ("enter grade \n");
    scanF ("%F",& B_M);
    if ((B_M>=8) && (B_M <=10))
    {
    printF (" Αρίστευσες \n");
    }
    else if ((B_M>=5) && (B_M <=8))
    {
    printF(" Πέρασες \n");
    }
    else if ((B_M>=4,5) && (B_M<=5))
    {
    printF ("Πέρασες με επάρκεια \n");
    }
    else
    {
    printF ("Ατύχησες\n");
    }
    System ("PAUSE")
    Return 0;
    I already solved some errors but i can't really compile it yet i get some errors on main. Help is appreciated.
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    The syntax for function is very specific. It's the same for main:
    Code:
    int main()
    {
        // the code for main
        return 0;
    }
    Please note the placement of the braces (ie, the { and the } ).

    C is case-sensitive. That means that it really matters which letters you capitalize and which you don't -- "uppercase" and "lowercase" are what "case" is about. Change the case of a single letter and you have an entirely different identifier. scanf is a function that you are trying to call, but scanF is something entirely different that hasn't been defined. Same thing with printF and System; they don't mean anything at all, yet, but printf and system do mean something. Same thing with Return.

    And please use code tags to preserve your code's indentation. Look above for an example.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by dwise1_aol
    The syntax for function is very specific. It's the same for main:
    Code:
    int main()
    {
        // the code for main
        return 0;
    }
    Please note the placement of the braces (ie, the { and the } ).

    C is case-sensitive. That means that it really matters which letters you capitalize and which you don't -- "uppercase" and "lowercase" are what "case" is about. Change the case of a single letter and you have an entirely different identifier. scanf is a function that you are trying to call, but scanF is something entirely different that hasn't been defined. Same thing with printF and System; they don't mean anything at all, yet, but printf and system do mean something. Same thing with Return.

    And please use code tags to preserve your code's indentation. Look above for an example.
    I see thanks it worked now but the problem is that when i type grade it prints me the result and in all numbers it's failed also the dos windows closes instanly. How i can keep it open ?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int main()
    {  float (B_M);
       printf ("enter grade \n");    
       scanf ("%F",& B_M); 
       if ((B_M>=8) && (B_M<=10))
       {
       printf ("Perfect \n");
       }
       else if ((B_M>=5) && (B_M<=8))
       {
       printf ("Good \n");
       }
       else if ((B_M>=4,5) && (B_M<=5))
       {
       printf ("Almost Passed \n");
       }
       else
       {
       printf ("Failed \n");
       }
       return 0;
    }
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    3
    Rep Power
    0
    if B_M is suppose to be a float..then try using 8.0 instead of 8 and so on to make the comparison...

    Also to keep the window open use the following line right before the return line in main() :

    system("pause");

    I think you also need to enter the header <stdlib.h>
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by fyah17
    if B_M is suppose to be a float..then try using 8.0 instead of 8 and so on to make the comparison...

    Also to keep the window open use the following line right before the return line in main() :

    system("pause");

    I think you also need to enter the header <stdlib.h>
    Oups sorry about the library i had it on dev but not here.Thanks now the dos window stays open.And i fixed the problem with failed. I had to put to scanf ("%f",& B_M); not scanf ("%F",& B_M);

    Thanks for the help :) I will be back with more questions soon!
  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Manila, Philippines
    Posts
    64
    Rep Power
    5
    I'm self-studying C over the summer and I'm trying my hand at this school exercise. :p
    Code:
    // Program for evaluating grades
    
    #include <stdio.h>
    
    int main (void)
    {
      int iGrade;
      
      printf ("Enter grade: ");    
      scanf ("%i", &iGrade); 
    
      if (iGrade == 10)
        printf ("Perfect!");
      else if (iGrade == 8 || iGrade == 9)
        printf ("Very good!");
      else if (iGrade == 6 || iGrade == 7)
        printf ("Good!");
      else if (iGrade == 5)
        printf ("Passed!");
      else if (iGrade >= 0 && iGrade <= 4)
        printf ("Failed!");
      else
        printf ("Invalid input!");
    
      return 0;
    }
    Can it still be optimized/shortened? I'm only considering entering whole numbers for this code.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    4
    Rep Power
    0
    Originally Posted by kathyrollo
    I'm self-studying C over the summer and I'm trying my hand at this school exercise. :p
    Code:
    // Program for evaluating grades
    
    #include <stdio.h>
    
    int main (void)
    {
      int iGrade;
      
      printf ("Enter grade: ");    
      scanf ("%i", &iGrade); 
    
      if (iGrade == 10) {
        printf ("Perfect!");
      } else if (iGrade == 8 || iGrade == 9) {
        printf ("Very good!");
      } else if (iGrade == 6 || iGrade == 7) {
        printf ("Good!");
      } else if (iGrade == 5) {
        printf ("Passed!");
      } else if (iGrade >= 0 && iGrade <= 4) {
        printf ("Failed!");
      } else {
        printf ("Invalid input!");
      }
      
      return 0;
    }
    Can it still be optimized/shortened? I'm only considering entering whole numbers for this code.
    Yes it can. What if someone has scored 6,6 your code will say invalid input.

    To catch numbers like 7,7 use float igrade; instead of int. Also make sure that you will use scanf ("%f", &iGrade); and add in the beggining of your code main() only. Use <= symbols to catch the real numbers on if. The character f in scanf stands for real numbers. And use system ("pause"); just before return so you can hold the window open after you run it on a compiler.Ohh also you should include on printf command \n... Example printf("lame \n"); The symbol \n tells the code to continue to the next line.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Manila, Philippines
    Posts
    64
    Rep Power
    5
    Hi validcode :)

    "I'm only considering entering whole numbers for this code."

    The code I posted will drop the decimal and actually read 6.6 and 7.7 as 6 and 7 respectively, therefore it will print "Good!", which is still correct.

    Originally Posted by validcode
    The character f in scanf stands for real numbers.
    I intentionally didn't include floating-point values. %f is a format character for decimal numbers, not necessarily real numbers. If you notice, the code is meant to read grades from 0-10 only, hence int. Otherwise, it will return "Invalid input!" By optimizing, I was wondering if I can minimize the conditions needed.

    Originally Posted by validcode
    And use system ("pause"); just before return so you can hold the window open after you run it on a compiler.
    I believe you're using the really old and discontinued version of Dev-C++, which has not been updated since 2005. It is strongly suggested to switch to one of the updated forks of the project if you want to continue using Dev-C++. I'm personally using Orwell Dev-C++, which is in active development. The issue with the window closing too fast has been addressed there, so you won't be needing system("pause"). There are also other compilers like Code::Blocks and wxDev-C++.

    You can also run your program through the cmd line, the way it's meant to be run.

    Originally Posted by validcode
    Ohh also you should include on printf command \n... Example printf("lame \n"); The symbol \n tells the code to continue to the next line.
    The \n is simply a preference.

    Originally Posted by validcode
    ...when i type grade it prints me the result and in all numbers it's failed...
    From what I see in your code from Post#3, the grades are all returning failed because you used the logical AND operator &&, which requires both conditions to be true. So if one condition is false, the statement is false, therefore it will execute your else-block "Failed!" instead. The logical OR operator || returns true when at least one of the conditions is true. You can run my code to check how it works.
  16. #9
  17. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,255
    Rep Power
    2222
    Originally Posted by validcode
    Yes it can. What if someone has scored 6,6 your code will say invalid input.

    To catch numbers like 7,7 use float igrade; ...
    One of the inherent problems with programming is that it's a second language in more ways than one. The human language being used is English, the alphabet is Roman (I've seen some Russian code with the code in Roman and the comments in Cyrillics), and the numeric notation is American.

    In writing floating-point values, some nations write them with a comma, which is what you appear to be doing, while some write them with a period. In the USA, we write them with a period, so in C and every other programming language I've encountered they are written with a period. It appears that kathyrollo understood what you meant, but the compiler doesn't. So regardless of what you write in your own language, in C you must use a period.

    Which leads us to this line of code that I haven't seen anyone comment on:
    else if ((B_M>=4,5) && (B_M<=5))
    Are we to assume that you had intended to write (B_M>=4.5)?

    What you had written caused a greater problem than you may think, because C has a "comma operator". In C, you can create compound expressions. We most commonly use it in for statements; eg:
    for (i=0, j=1; i < 10; i++, j+=2)
    In that for statement, the semicolon (;) separates the three expressions, while the comma (,) allows you to do more than one thing in each expression. In this example, you get to initialize more than one control variable and you get to iterate more than one variable. Since every expression evaluates to a value, an expression that uses the comma operator evaluates to the last section. So i=0,j=1 would evaluate to 1 while i++, j+=2 would evaluate to j+2. Do you see how that works?

    Now lets look at your statement again:
    else if ((B_M>=4,5) && (B_M<=5))
    See that (B_M>=4,5) is an expression that uses a comma operator. What does it evaluate to? The answer is 5. What does that mean? As you will recall, in a conditional expression a value of zero means false while a non-zero value means true. Since (B_M>=4,5) always evaluates to 5 regardless of the value of B_M and since 5 is always non-zero, that means that that expression will always be true. While will give you false results.
  18. #10
  19. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,995
    Rep Power
    481

    To KathyRollo


    To let you know that there are other ways to write your program, I share this code.

    What's wrong with your program? Nothing that I can tell, but only because it is so short. Your program is much easier to read than my submission. On the other hand, your program smells badly. You have duplicate code, with all those printf statements. If you wanted to do something else with the chosen string you'd have to change your program in 6 places. And your program is harder to prove because there are so many paths through it.

    My BIND macro has several paths, but can be proven independently. I included a code path to test the validity of scanf. Then again, my program works for
    echo x | ./myVersion
    whereas
    echo x | ./yourVersion
    reports "Failed!". I separated the computation of the string from how that string is used, even if it looks like they are in the same statement.
    Code:
    #include <stdio.h>
    
    #define TAU (2*M_PI)  /* http://www.youtube.com/watch?v=jG7vhMMXagQ */
    
    #define BIND(A,L,H) ((L)<(A)?(A)<(H)?(A):(H):(L))
    
    char*comment[] = {
      "Perfect!","Very good!","Good!","Passed!","Failed!","Invalid input!"
    };
    
    int indexes[] = {5,4,4,4,4,4,3,2,2,1,1,0,5};
    
    int main(void) {
      int iGrade;
      long myGrade;
      fputs("Enter grade: ",stdout);
      if (1 != scanf("%i", &iGrade))
        iGrade = 666;
      myGrade = iGrade + 1L;
      puts(comment[indexes[BIND(myGrade,0,12)]]);
      return 0;
    }
    The notion of "optimization" seems silly to me for such a short if-then-else sequence. To optimize a computer program I'd start by profiling the common slow use cases and then look for algorithmic improvements in the sections of the code that consume the most cycles/resources. Yes, this program may explain my lack of friends.
    [code]Code tags[/code] are essential for python code and Makefiles!
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Location
    Manila, Philippines
    Posts
    64
    Rep Power
    5
    @b49P23TIvg:
    I have only been self-studying C for a few weeks so your submission is Greek to me haha. But hey, thanks for showing there's such a way to do things.

IMN logo majestic logo threadwatch logo seochat tools logo