-
I am VERY new at c and I have a very quick and easy question. I have an assigment at school to simply display some information on the command line (command line app). The problem I have is that at the end of it I have to ask, do you really want to quit? I want to scan for yes and if anythying else don't quit. The problem occurs when a user types something in, even yes, instead of quitting it will then say "Press any key to continue". Here is my code....Any suggestions????
// ws1.cpp : Defines the entry point for the console application.
//
//this is the include statement for the header file
#include "stdio.h"
//states that main is the name of the function, it uses no arg's
//and it will return an integer
int main(void)
{
//these statements uses the /t function which "tabs"
//whatever is being displayed by printf
printf("\t\t\t\tCurrency \n");
printf("\t\t\t\tConversion \n");
printf("Pound");
printf("\t\t\t\t\t\t\t1.670 Dollars\n");
printf("Franc");
printf("\t\t\t\t\t\t\t .100 Dollars\n");
printf("Lire");
printf("\t\t\t\t\t\t\t .020 Dollars\n");
printf("Krona");
printf("\t\t\t\t\t\t\t .100 Dollars\n");
printf("Mark");
printf("\t\t\t\t\t\t\t .252 Dollars\n");
//then print a sentence asking the user of they would like to quit
printf("Do you really want to quit (Yes/No)?\n");
//scan for yes, if no don't quit
scanf("yes");
return 0;
}
Also is there any function to center, or right or left justify????
-
I think you're using Visual C++. The "Press any key" at the end isn't part of your program - it adds it when you call the program through the IDE. It's so that you can easily see the output from the program, otherwise the window closes :(.
-
Your right on the C++ part and I see what you are saying abou the reason for the press any key. Is there a way to take that off and have it close only is "Yes" is typed in? Also is there anyway to center or right or left justify the text instead of using the /t method?
-
The program HAS finished. Run the program from explorer and you'll see what I mean. I'm not sure about centring the text. I'd have to get back to you on that one.
-
Ok, OK I see what you are saying....Is there a way though to only exit if Yes is typed in?
-
Try this:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char buf[50]; // To receive user input
printf("Program!\n\n");
for(;;) { // Loop forever - until told to stop
printf("Do you want to exit (Y/N)?"); // Ask question
scanf("%c", buf); // Get input
_strupr(buf); // Change to uppercase for comparison
printf("\n");
if(!strcmp(buf, "Y")) { // Check if they're the same
printf("Exiting!\n");
break; // Break out of for(;;) loop
}
fflush(stdin); // Clear all extra input
}
return 0;
}
The thing to consider with strcmp is that if they are the same, it returns 0.