Results 1 to 2 of 2

Thread: First C Lecture, Insanity remade

  1. #1
    Join Date
    Dec 2011
    Location
    -bash
    Posts
    515
    Mentioned
    0 Post(s)
    Quoted
    27 Post(s)

    Lightbulb First C Lecture, Insanity remade

    This is the first program we wrote today and I need help understanding some stuff cause I am used to c++

    C Code:
    #include <cstdio>
    #include <cstdlib>
    #include <time.h>
    using namespace std;

    int main(void)
    {
        int deja;
        int num1, num2;

        printf("This program  gets 2 numbers from the user and randomly adds or subtracts them \n");
        printf("Enter 2 numbers and hit the ENTER key after each one \n");

        scanf("%i", &num1);
        scanf("%i", &num2);

        printf("%s, %i, %i \n", "The 2 numbers are : ", num1, num2);
        srand(time(NULL));
        deja = rand()%2;

        if (deja == 1)
        {
            printf("%s, %i \n","The sum is", num1 + num2);
        }
        else
        {
            printf("%s, %i \n", "The difference is" ,num1 - num2);
        }

        return EXIT_SUCCESS;
    }

    Everytime I run it, it does not print these first 2 lines
    C Code:
    printf("This program  gets 2 numbers from the user and randomly adds or subtracts them \n");
    printf("Enter 2 numbers and hit the ENTER key after each one \n");

    Instead goes to the scanf and eventually waits for the user to enter both numbers before displaying the first 2 printf functions.

    Also is there a different way to do the rand function that is easier to understand to someone who has never used it before?

    When it runs, it shows this:
    Code:
    This program  gets 2 numbers from the user and randomly adds or subtracts them 
    Enter 2 numbers and hit the ENTER key after each one 
    The 2 numbers are : , 6, 5 
    The difference is, 1
    Is there a way to prevent it from showing the comma before the numbers?

  2. #2
    Join Date
    Feb 2011
    Location
    The Future.
    Posts
    5,600
    Mentioned
    396 Post(s)
    Quoted
    1598 Post(s)

    Default

    One.. You're still writing C++ code.

    Two.. You're includes are C++ includes. All C includes end in .h. Any include prepended with C is a C++ version of the C include.

    Three.. To remove the comma, don't prepend the value with it:

    C Code:
    #include <stdio.h>  //C++ :  CSTDIO
    #include <stdlib.h> //C++: CSTDLIB
    #include <time.h>  //C++ : CTIME

    //using namespace std;

    int main(void)
    {
        int deja;
        int num1, num2;

        printf("This program  gets 2 numbers from the user and randomly adds or subtracts them \n");
        printf("Enter 2 numbers and hit the ENTER key after each one \n");

        scanf("%i", &num1);
        scanf("%i", &num2);

        printf("%s %i, %i \n", "The 2 numbers are:", num1, num2);
        srand(time(NULL));
        deja = rand()%2;

        if (deja == 1)
        {
            printf("%s %i \n","The sum is:", num1 + num2);
        }
        else
        {
            printf("%s %i \n", "The difference is:" ,num1 - num2);
        }

        return EXIT_SUCCESS;
    }
    I am Ggzz..
    Hackintosher

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •