checking if digit only in C

This is a discussion on checking if digit only in C within the C Programming forums, part of the General Programming Boards category; Hello, I must make a program, were I have to enter numbers after which my program will calculate exam mark ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    5

    checking if digit only in C

    Hello, I must make a program, were I have to enter numbers after which my program will calculate exam mark according to these marks. In the start i wrote program in C language, but I didn't know how to write functions which would work, I had some ideas ( if and while ) but they didn't work, then I found a code sample in C++ and wrote program in this language and it worked, but then I found out, that it is not allowed to write program with C++ language according to my task. I would be very grateful if someone could help me to rewrite my code in C language:
    Code:
    # include <stdio.h>
    # include <conio.h>
    # include <ctype.h>
    # include <iostream> 
    
    float A,formula1,formula2;
    using namespace std;
    int main()
    {
    int Ae;
    cout << "Enter exam mark: ";
    cin >> Ae; 
    while (!cin.good())
    {
    cin.clear ();
    cin.ignore (256, '\n');
    cout << "Enter a number: ";
    cin >> Ae;
    };
    cout << "Data is correct: " << Ae << endl;
    int Alab;
    cout << "Enter laboratory work mark: ";
    cin >> Alab;
    while (!cin.good())
    {
    cin.clear ();
    cin.ignore (256, '\n');
    cout << "Enter a number: ";
    cin >> Alab;
    };
    cout << "Data is correct: " << Alab << endl;
    int Aref;
    cout << "Enter report mark: ";
    cin >> Aref;
    while (!cin.good())
    {
    cin.clear ();
    cin.ignore (256, '\n');
    cout << "Enter a number: ";
    cin >> Aref;
    };
    cout << "Data is correct: " << Aref << endl;
    formula1=((0.6*Ae)+(0.3*Alab)+(0.1*Aref));
    formula2=(Ae+(0.3*Alab)+(0.1*Aref));
    if (Ae >= 4) A=formula1; else A=formula2;
    printf("%2.1f - Exam mark\n",A);
    getche();
    return 0;
    }
    And I also forgot to define that there can be entered numbers only from 0 to 10 ( because they are marks)

    If someone could give me a little help, I would be very grateful

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    Code:
    #include <stdio.h>
    
    int main()
    {
        fprintf(stdout, "Do your own homework\n");
        return 0;
    }
    Right 98% of the time, and don't care about the other 3%.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Grumpy *has* to be grumpy, or they'll take that name away from him.

    Anybody who posts code using code tags, on their very first post, gets some props from me.

    Code:
    # include <stdio.h>
    # include <ctype.h>
    //you may not have conio.h, and I doubt this program needs it.
    # include <conio.h>  
    
    float A,formula1,formula2;
    int main() {
      int Ae;     //cout << "Enter exam mark: ";
      printf("\nEnter exam mark: ");
      scanf("%d", &Ae);
      (void) getchar();      //cin >> Ae; 
    
      while (Ae > 0)   {     //cin.clear (); //   cin.ignore (256, '\n');
        printf("\nEnter a number: ");    //cout << "Enter a number: ";
        scanf("%d", &Ae);  //cin >> Ae;
        (void) getchar();
      };
                                //cout << "Data is correct: " << Ae << endl;
      printf("\nData is correct: %d\n", Ae);
      int Alab;
                             //cout << "Enter laboratory work mark: ";
      printf("Enter laboratory work mark: ");
      scanf("%d", &Alab);   //cin >> Alab;
      (void) getchar();
                                   //while (!cin.good()) {
      while (Alab > 0) {     //negative numbers quit the loop
                                  //cin.clear (); //cin.ignore (256, '\n');
        printf("\nEnter a number [<0 to quit] :");    //cout << "Enter a number: ";
        scanf("%d", &Alab);  //cin >> Alab;
          (void) getchar();
      };
      cout << "Data is correct: " << Alab << endl;
      int Aref;
      cout << "Enter report mark: ";
      cin >> Aref;
      while (!cin.good())  {
        cin.clear ();
        cin.ignore (256, '\n');
        cout << "Enter a number: ";
        cin >> Aref;
      };
      cout << "Data is correct: " << Aref << endl;
      formula1=((0.6*Ae)+(0.3*Alab)+(0.1*Aref));
      formula2=(Ae+(0.3*Alab)+(0.1*Aref));
      if (Ae >= 4) A=formula1; else A=formula2;
      
      printf("%2.1f - Exam mark\n",A);
      getche();
      return 0;
    }
    That will give you a start. The forumula doesn't look good, however. 0.6*Ae, when Ae is an integer, looks wrong - might need to change it to a float or a double.

    And Welcome to the forum, Nuubik!

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,252
    Quote Originally Posted by Adak View Post
    Grumpy *has* to be grumpy, or they'll take that name away from him.
    I don't have to be grumpy. It just works out that way.

    But come on! The original poster is openly doing a homework exercise, and has either found a code sample (presumably from someone who did the course previously who was marked down for mixing C and C++) or has hacked together some bits of code without real thought about how the bits interact.

    Incidentally, depending on age of C compiler, your approach won't work either.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    I tend to agree with grumpy here, I mean although he did use code tags an offence of copying homework from another source is just as bad from a functional point of view even if you admit to it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  6. #6
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    No "approach" intended. Just a nudge in the right direction from C++ to C. I certainly did not try to compile it.

    It might offend our sensitivities,(life does that), but we learn by doing. He's not out getting ........-faced, and he'll learn a bit. If he could spend the time and learn the way we'd like him to, he wouldn't be posting stuff like that, admittedly.

    He did say he wrote a program for it, in C, but it didn't work, so then he found this one in C++. After it's fully modified for C, he will have learned a few things, anyway.
    Last edited by Adak; 10-02-2010 at 06:39 AM.

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Since itīs cross-posted, thereīs no point 2 forums wasting time on it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 03:58 PM
  2. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Checking if characters is a valid digit
    By bigmac(rexdale) in forum C Programming
    Replies: 8
    Last Post: 05-14-2008, 07:12 AM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21