Hi I'm currently using C. I need help. It won't let me re-run my program. After the compiler outputs "Re-run?", it won't even let me input a character.
#include <Windows.h> // to pause a certain part of program. Sleep (number of seconds here);
#include <stdio.h>
#include <stdlib.h>
int main ()
{
char user [30]; //name
char ask ;
unsigned age = 0;
int inputnum;
int convert = 0;
int speed = 0;
system ("color 0A"); // changes color of text and backround as well
do
{
printf("Enter your name: ");
scanf_s("%s", user, 30);
printf ("\n");
printf ("Enter your age: ");
scanf_s("%u", &age);
printf ("\n");
if (age > 40)
{
printf ("You're old %s\n", user);
}
else if (age > 20 && age < 40)
{
printf ("You're an adult %s\n", user);
}
else if (age > 0 && age < 20)
{
printf ("You're still young %s\n", user);
}
if (!age)
{
printf ("Error. Not a number\n");
printf ("Now exiting....");
Sleep (2000);
exit(1);
}
printf ("\n");
printf ("How fast do you generally drive (MPH)?\n");
scanf_s("%i", &convert);
printf ("\n");
speed = convert * 1.6;
printf ("%s, your kilometers per hour is: %i KPH\n", user, speed);
printf ("\n");
First of all please use [code]// your code here [/code] tags around your source code.
Secondly, please keep your code compact. Waste as little vertical space as possible.
Let's see now. If scanf_s() doesn't work may I suggest that you use old scanf() instead? For instance I can't even compile your code because my compiler doesn't know what scanf_s() is.
My compiler tells you don't use the variable inputnum and that you don't return 0; from your main().
According to MSDN you need an extra paramter that tells the size
MSDN wrote:
Unlike scanf and wscanf, scanf_s and wscanf_s require the buffer size to be specified for all input parameters of type c, C, s, S, or [. The buffer size is passed as an additional parameter immediately following the pointer to the buffer or variable. [...]
In the case of characters, one may read a single character as follows:
You probably have a rouge char hanging around in the input .
try:
1 2 3 4
printf ("Re-run?");
scanf_s("%*[^\n]"); /* Clear every thing upto the next \n */
scanf_s("%*c"); /* Eat the \n */
scanf_s("%c", &ask,1); /* Get user input */