Why doesen't it clear screen in if method (C language only) - c

I was trying to make a shell interpreter similar to DOS in C language (for fun obviously)
And when i type clear, as shown in the code below it should make it so it clears the screen. But it doesn't.
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
char command[128];
int loop = 0;
void main(){
clrscr();
printf("Starting shell\n");
clrscr();
while ( loop == 0){
printf("command:");
scanf("%s", &command);
if(command=='clear'){
printf("Clearing screen");
clrscr();
}
/** Other Code **/

if(command=='clear')
it's not valid string comparison. use strcmp to compare string in C.
It should be
if (!strcmp(command, "clear"))
{
printf("Clearing screen");
clrscr();
}

Related

program not waiting for input in c

#include <cs50.h>
#include <stdio.h>
int main(void) {
string answer = get_string("what is your name? ");
printf("%s\n", answer);
}
I have a program with this code, that is running well, every other program that asks for input does not run rightly. I am on windows 10, I used MINGW to compile the program.
You could try it this way
#include<string.h>
#include <stdio.h>
int main(void) {
char answer[20];
printf("What is your name");
gets(name);
printf("%s\n", answer);
}

C Program Crashes in Visual Studio

I am getting this error whenever I run my code in visual Studio:
#include <stdio.h>
#include <ctype.h>
int main() {
char username[10];
printf("Enter Username: ");
scanf_s("%[^\n]", &username);
while (isupper(username)) {
if (username == '-') {
printf("Username cannot contain UpperCase Letters");
}
}
}
Error Image
I don't think you can pass whole array to isupper. Also if you don't want to return anything instead of int main() use void main() or just return 0 in the end or when you want to end after your program executed successfully. As for using scan_s or scanf or getline or whatever I won't say anything because its a different matter and your syntax of scanf_s is certainly wrong.
Also following code will not check for any buffer overflow (not a good practice, you will see even though we gave size 20 char array, this code will work even for larger input which is certainly not a good thing). So you can either limit the size of input or better to read an entire line via fgets() (or getline() if available) and parse the string yourself.
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(){
char username[20];
printf("Enter Username: ");
// scanf("%[^\n]", username); <--- Instead of this
scanf_s("%20c", username, 20); // <----Try Using this
int i=0;
while (i<strlen(username)) {
if (isupper(username[i])) {
printf("Username cannot contain UpperCase Letters\n");
return 0;
}
i++;
}
return 0;
}
My first guess would be that your while is an endless loop, try to do it like this:
int i;
for(i=0; i<strlen(username);i++){
if(isupper(username[i])){
printf("Username cannot contain UpperCase Letters");
}
}

What is the issue with the following code? [duplicate]

This question already has answers here:
Issue with main arguments handling
(3 answers)
Closed 6 years ago.
I am learning C on my own but this code which seems right to me doesn't works right
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
char name[20];
int p,c,m;
printf("Enter your name \n");
scanf(" %s", name);
if ( (name=='luv') || (name='pranav') )
{
printf("Enter your marks in pcm \n");
}
else
{
printf("get lost");
}
getch();
}
I want the correct code to run only if I enter name as luv or pranav but instead what is happening is that no matter whatever name i type it is running the code under else and i am not able to figure out the reason.
I am using codeblocks as compiler.
You cannot compare strings using ==, to compare strings, one has to use strcmp()
strcmp() returns 0 when the strings are same, other wise it returns the difference of those two strings,
So essentially, your code would become,
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // for the strcmp() function
main()
{
char name[20];
int p,c,m;
printf("Enter your name \n");
scanf(" %s", name);
// strings are given inbetween double quotes
// characters are given inbetween single quotes
if ( !(strcmp(name, "luv")) || !(strcmp(name, "pranav")) )
{
printf("Enter your marks in pcm \n");
}
else
{
printf("get lost");
}
getch();
}
NOTE:
1) Use the standard definition of main()
int main(void) //if no command line arguments.
2) Check the return of functions like scanf().
Lots of mistakes in the code.
I am trying to fix and show:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // to use strcmp
int main(void) // int and void added
{
char name[20];
int p,c,m;
printf("Enter your name \n");
scanf("%19s", name); // no space before % and 19 to limit input
if ( !strcmp(name,"luv") // " instead of ' , and strcmp with operator !
|| strcmp(name,"pranav") == 0 ) // instead of ! you can use == 0
{
printf("Enter your marks in pcm \n");
}
else
{
printf("get lost");
}
getch();
}
main() is not a standard signature. You should use standard int main(void) unless you have some special reason to use non-standard signature.
'luv' and 'pranav' are multiple-character character constant, which have implementation-defined values. You should use string literals and strcmp() function.
name='pranav' is an assignment, and you cannot assign to what is converted from arrays, so this will emit compile error.
You should limit the length to read in order to avoid buffer overflow.
Try this:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char name[20];
int p,c,m;
printf("Enter your name \n");
scanf(" %19s", name);
if ( (strcmp(name, "luv") == 0) || (strcmp(name, "pranav") == 0) )
{
printf("Enter your marks in pcm \n");
}
else
{
printf("get lost");
}
getch();
}
remove #include <conio.h> and getch(); if they are not supported.
Two problems:
Single quotes are used for character constants, not string constants. You need to use double quotes for those.
Strings can't be compared with ==. What you're actually doing is comparing the address of the first element of name with a character constant. Even if you fixed the quotes on the constant, you'd be comparing the address of name with the address of a string constant, which are not the same. To compare strings, you use strcmp, which compares each character in the string.
So what you want is this:
if ( (strcmp(name,"luv") == 0) || (strcmp(name,"pranav") == 0) )
You'll also need to #include <string.h> to use strcmp.

Trying to call custom function in C but scanf( ) is causing an issue

Can you tell what wrong here?
#include <stdio.h>
#include <stdlib.h>
int test (void)
{
int i;
printf("Enter a number: ");
scanf("%d",&i);
return i;
}
int main (void)
{
test();
return 0;
}
This is just a simple example but for some reason main doesn't run unless I get rid of the scanf.
Always use a '\n' at the end of your printf string. This makes the output buffer flush and print the string. Add more prints in your program.
You can rewrite your program like following, and the prints will help you understand what is happening with your program.
#include <stdio.h>
#include <stdlib.h>
int test (void)
{
int i;
printf("Enter a number: \n");
scanf("%d",&i);
printf("You just eneterd : %d\n",i);
return i;
}
int main (void)
{
printf("About to call test() \n");
test();
printf("Done calling test() \n");
return 0;
}
Better get a good C programming book for understanding these basic stuff. I suggest The C programming language
I think you must make use of fflush() or making use of a '\n' character at the end of the printf function which will eventually flush the std output buffer. For checking just make use of the printf() for printing the value of the variable just after reading the value.
Hope that helps....

String input using getchar()

The following code uses getchar() to accept a line of input.
#include <stdio.h>
#include <stdlib.h>
int main()
{
char *rawString = (char *)malloc(200*sizeof(char));
char *rawStringInitial = rawString;
char c;
c=getchar();
while(c!='\n')
{
*rawString=c;
rawString++;
c=getchar();
}
*rawString='\0';
printf("\n[%s]\n",rawStringInitial);
return(0);
}
While typing, if I press backspace, shouldn't it also be received by getchar() & stored in the rawString-pointed location? However the output simply shows the final string without any special characters. Could someone explain why?
Standard input is (usually) buffered; non-printing characters like backspace are handled by the terminal server, and library functions like getchar() will never see them.
If you need to read raw keystrokes, then you will need to use something outside of the C standard library.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void get_string(char *string);
void main(){
char *stringVar;
clrscr();
printf("Enter String : ");
get_string(stringVar);
printf("String Enter : %s",stringVar);
getch();
}
void get_string(char *string){
char press;int i=0;
do{
press=getch();
if(press!=8){
printf("%c",press);
string[i]=press;
i++;
}
else if(i>0){printf("\b%c\b",0);sting[i]=NULL;i--;}
}while(press!13);
}
This is Will Work.

Resources