Jump to content

Getting C to pause after outputting

- - - - -

  • Please log in to reply
23 replies to this topic

#1
Jamie_B

Jamie_B

    Newbie

  • Members
  • PipPip
  • 10 posts
Hi guys, first post here, I know it's very "nooby" but please bear with me.

I have had a look around on other forums and lots of googling, tried about 5 different lines of code to try and get this thing to pause after outputting but It just won't work.

I'm using Dev-C++ on Windows 7 64bit.

#include <stdio.h>


int main()

{

    int b;

    printf("Enter a value:");

    scanf("%d", &b);

    if (b < 0)

        printf("The value is negative\n");

    getchar();

    return 0;

}


As you can see my most recent attempt was to use getchar to pause it but after I enter the number, It just outputs "the value is negative" and closes almost instantly, barely enough time to read it.

P.S First time learning C or any language for that matter.

Anyone got any ideas how I can get it to wait for me to hit a key and then exit?

Thanks and sorry again.

#2
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Scanf unfortunately only accepts the integer, when you press <enter> it creates a newline as well and that skips the scanf completely plugging into your getchar(). You can place this after your scanf statement:
getc(stdin);
This will eat the newline.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#3
Jamie_B

Jamie_B

    Newbie

  • Members
  • PipPip
  • 10 posts
Thanks nullw0rm that worked perfectly.

I have a new problem though :/
#include <stdio.h>

int main()
{
    int a, b, c, e;
    printf("Enter a Number");
    printf("First Number:");
    scanf("%d", &a);
    printf("Second Number:");
    scanf("%d", &b);
    printf("Third Number");
    scanf("%d", &e);
    c = a + b + e;
    printf("%d + %d + %d = %d\n", a, b, e, c);
    getchar();
    getc(stdin);
    return 0;
}

How can I get this code to print those 2 printf's on seperate lines?

At the moment they just print on the same line like this "Enter A NumberFirst Number"

Thanks guys

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
\n is escape sequaence for new line.

printf("\n"); // will print a new line

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
Jamie_B

Jamie_B

    Newbie

  • Members
  • PipPip
  • 10 posts
Thanks alot :)

#6
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts

#include <stdio.h>

#include <conio.h>


int main()

{

    int a, b, c, e;

    printf("Enter a Number\n");

    printf("First Number:");

    scanf("%d", &a);

    printf("\nSecond Number:");

    scanf("%d", &b);

    printf("\nThird Number");

    scanf("%d", &e);

    c = a + b + e;

    printf("%d + %d + %d = %d\n", a, b, e, c);

    getch();

    return 0;

}


Try this. Here, in conio.h library you can write only getch(). And \n - means new line. \t - means tab. Try it.

#7
Jamie_B

Jamie_B

    Newbie

  • Members
  • PipPip
  • 10 posts

xle_camry said:


#include <stdio.h>

#include <conio.h>


int main()

{

    int a, b, c, e;

    printf("Enter a Number\n");

    printf("First Number:");

    scanf("%d", &a);

    printf("\nSecond Number:");

    scanf("%d", &b);

    printf("\nThird Number");

    scanf("%d", &e);

    c = a + b + e;

    printf("%d + %d + %d = %d\n", a, b, e, c);

    getch();

    return 0;

}


Try this. Here, in conio.h library you can write only getch(). And \n - means new line. \t - means tab. Try it.

Oh wow that is cool, Thanks alot!

BTW I don't fully understand libaries, I know stdio.h is something to do with letting people use the keyboard to input characters but thats about it?

Do you have a link or something I can read into it about?

#8
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
O.o
C standard library - Wikipedia, the free encyclopedia

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#9
xle_camry

xle_camry

    Programmer

  • Members
  • PipPipPipPip
  • 141 posts
Most useful libraries are stdio.h, conio.h, string.h, math.h. You can find them in c books. If you want i can send you some useful c books, just post me your email address, by writing me privately.

#10
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
conio.h is bad, or so I've been told. Another cool escape character is \r (carrige return) aswell.
void wait(time_t msec) {
    time_t end = clock() + msec;
    while (clock() < end) { }
}

int main() {
    char tab[] = {'*', '.', '#', '$', '~'};
    for (int j = 0; j < 5; ++j) {
        for (int i = 0; i < 20; ++i) {
            printf("%c", tab[j]);
            wait(50);
        }
        printf(" some text \r");
    }
    return 0;
}

A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#11
Jamie_B

Jamie_B

    Newbie

  • Members
  • PipPip
  • 10 posts

Flying Dutchman said:

conio.h is bad, or so I've been told. Another cool escape character is \r (carrige return) aswell.


#include <stdio.h>

int main()
{
    int b;
    printf("Enter a value:");
    scanf("%d", &b);
    if (b < 0)
        printf("The value is negative\n");
    else if (b == 0)
        printf("The value is zero\n");
    else
        printf("The value is positive\r");
    return 0;
}

Am I doing something wrong?

It's still closing with /r.

#12
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
You've been told wrong.
Of course, conio.h is Windows only, because it was originally made for DOS, but it's still very useful - it has many functions that I can't live without, such as -

getch()/getche() - unbuffered getchar().
textcolor() - lol, sets the text color.

It's useless on *nix, which has different header files. Still, you can't just say it's bad.

Posted Image
There is no problem that cannot be solved by the use of high explosives.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users