Thread: Very simple codes in C,Need help!!

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    17

    Very simple codes in C,Need help!!

    Hello,

    I have some "issues" they are pretty simple,but somehow I cant write them ;

    =I am allowed to use While,if,else,<stdio.h>

    1-I got to recieve a series of grades 0-100 and when finishe enter 22,the problem is that I dont know how to do the input e.g:
    90 39 4 77 83 22,How am I supposed to scanf all these numbers in one line in the cmd?(when user finishes input is 22 then enter)

    =I got to check if numbers and input is legal

    Thank you
    Last edited by Kadmany; 03-25-2011 at 11:58 AM.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, we don't usually do people's homework for them...
    Read this --> C Board - Announcements in Forum : General Programming Boards

    However, if you get to work on your code and run into trouble, you can post the code here and I'm sure some of us will help out.

    (Hint: Look in your C library documentation at scanf() )

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    17
    Quote Originally Posted by CommonTater View Post
    Well, we don't usually do people's homework for them...
    Read this --> C Board - Announcements in Forum : General Programming Boards

    However, if you get to work on your code and run into trouble, you can post the code here and I'm sure some of us will help out.

    (Hint: Look in your C library documentation at scanf() )
    Its not you doing my homework,it is just I couldnt understand the thing with the EOF,I couldnt understand whats is the value of scanf when the input if illegal,dude I like searched the net for this,,,!

    Thank you

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, like search the documentation for your compiler's libraries...

    scanf - Wikipedia, the free encyclopedia <--- first return after search for "scanf()" on Google.

    And don't ever refer to me as "Dude"...

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    17
    Quote Originally Posted by CommonTater View Post
    Well, like search the documentation for your compiler's libraries...

    scanf - Wikipedia, the free encyclopedia <--- first return after search for "scanf()" on Google.

    And don't ever refer to me as "Dude"...
    first of all thank you,

    Second off,I am sorry for referring to you with the word "dude" but its just i thought it was a good word in english ,,,

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Kadmany View Post
    first of all thank you,
    I trust my non-answers were satisfactory ... actually the skill for looking stuf up is a pretty important part of the trade, so it's important you learn how to do it early on.

    Second off,I am sorry for referring to you with the word "dude" but its just i thought it was a good word in english ,,,
    It has different meanings on a regional basis... Some areas everyone is "Dude"... some areas it's a friendship thing "He's my Dude!"... where I am it's like calling someone an a-hole, quite the insult really. (One of the problems you run into on international sites like these)

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    17
    Hmm I read scanf() in wiki,but I still cant understand how to solve this,my only problem is to know how I could make scanf() recieve as input numbers with spaces between them and then stop at 22

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The general idea might look like this kind of logic: (this is not code)
    Code:
    do {
      print "enter a number or 22 when input is complete"
      scanf("your format", &yourNumber)
    }while(yourNumber does not equal twenty-two);

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Kadmany View Post
    Hmm I read scanf() in wiki,but I still cant understand how to solve this,my only problem is to know how I could make scanf() recieve as input numbers with spaces between them and then stop at 22
    Ok... looking at your original problem, I would not attempt to get all those inputs on one line... In fact from a practical standpoint it would be rather foolish to try.

    First; unless you are certain you will always get the same number of entries, scanf() is useless to you.

    Second; trying to read the group as a string using fgets() will work, but parsing the numerical values out of the string is some pretty advanced coding. Even for some of the more advanced programmers here, this is some pretty complex stuff.

    Third; since I take it you are pretty new at this, in your place I would be prone to question the assignment suggesting that inputs on multiple lines make more sense.

    Adak has given you a suggestion for how to handle this with a loop, and I think it's the best way to do it.

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    1. scanf returns the number of items successfully scanned.
    int count;
    count = scanf("%d",&val);
    if (count == 1) ...succeed
    if (count == 0) ...fail

    2. scanf for 1 item at a time will work with multiple items on a line. each call to scanf("%d",&val) will eat only the next number and the rest will still be there waiting for the next scanf. when it gets to the end of a line it will wait patiently for you to enter the next line and it will wake up and continue looking for the next number (or illegal string)

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by dmh2000 View Post
    1. scanf returns the number of items successfully scanned.
    Actually, it's the ones it stores. Consider:
    Code:
    stored = scanf( "%c%*c", &yorn );
    It should have a return value of 1, even though it actually scans 2 items.
    Quote Originally Posted by man page
    RETURN VALUES
    These functions return the number of input items assigned, which can be
    fewer than provided for, or even zero, in the event of a matching fail-
    ure. Zero indicates that, while there was input available, no conver-
    sions were assigned; typically this is due to an invalid input character,
    such as an alphabetic character for a `%d' conversion. The value EOF is
    returned if an input failure occurs before any conversion such as an end-
    of-file occurs. If an error or end-of-file occurs after conversion has
    begun, the number of conversions which were successfully completed is
    returned.

    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    17
    Hello,

    First of all,thank you all very much for helping me,Before I continue I want to stress that
    I didn't just throw a "Homework" on the net just because I am bored to do it ,as you all see it is not that clear how scanf() works,what value it returns,and of-course the EOF,

    so,to get this clear ;

    when I do a regular scanf(),(one command),for one string input ( without space) I will get a normal one input

    for two strings with space in between them, scanf() will only get first string?

    for 3 ,,,, n ,,,,, etc only first ?

    when I combine between while and scanf() ,as ;
    Code:
    while(grade!=EOF){
    
    EOF=scanf("%d",grade);
    
    sum+=grade;
    
    }
    ? is it correct

    it is just not that clear see these inputs :

    23 65 97 101
    231 45 101
    e 43 88 100 101
    t 343 23
    101
    101
    what will happen then ? How am I supposed to handle these errors ?

    Thank you all in advance!

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    17
    Code:
    #include <stdio.h>
    #define STOP_INPUT 101
    
    int cnt=0,check,scanf_result;
    double min=0,max=0,average=0,sum=0,grade;
    
    int main(){
    
      printf("Please enter the grades : ");
    
      while(grade!=STOP_INPUT){
    
          scanf_result=scanf("%lf", &grade);//We get inout from user and save what scanf itself return (if there is error ,,, )
    
          if(scanf_result<1){
    
              printf("\nYou entered ilegall value,all values must be numbers\n");
              //No need to check if its in range 0 - 100,because its given that numbers are only in range 0 - 100!
              return 1;
          }
    
          if(grade!=STOP_INPUT){
    
              sum+=grade;
    
              if(grade>max){
    
                  max=grade;
    
              }
    
              if(cnt==0){
    
                min=max;
    
              }
    
              if(grade<min){
    
                  min=grade;
              }
    
          cnt++;
    
          }
          else{//We check if grade=101
    
              if(cnt==0){//We check if cnt still equals 0,so it means this grade is the first which means user didnt enter any real grade!
    
                  printf("\nYou didn't enter any grade!");
              }
          }
    
       }
    
      if(cnt!=0){
    
      average=sum/(double)(cnt);
    
    
    
         check=average-average/10;//the methos average%10 DIDN'T work,it gave me error of operands! dont know whats this,so I used this.
        if(check==0){
    
           printf("\nYour average is : %d \n", (int)(average));
    
          }
          else{
    
            printf("\nYour average is : %.2lf  \n", average);
    
           }
    
      printf("Your Heighst grade is : %lf  \n", max);
      printf("Your Lowest grade is : %lf \n", min);
    
      }
    
      printf("\n");
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Simple Compile Time Problem - HELP!
    By kamikazeecows in forum Windows Programming
    Replies: 2
    Last Post: 12-02-2001, 01:30 PM