#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);
}
Related
I'm simply trying to write down something to console using C language before getting user input, but it always asks for input then when I press enter it print what I wanted to print before getting user input.
I'm using Eclipse IDE (version 2020 -09) and my compiler is Cygwin.
this is how I'm to trying to accomplish that:
#include <stdio.h>
int main() {
printf("please enter your name: ");
char a[3];
gets(a);
return 0;
}
Output is usually buffered by default.
You can flush the output buffer explicitly by calling fflush(stdout) after printf(), eg:
#include <stdio.h>
int main() {
printf("please enter your name: ");
fflush(stdout); // <-- here
char a[3];
gets(a);
return 0;
}
Or, you can include \n in the output, eg:
#include <stdio.h>
int main() {
printf("please enter your name: \n");
char a[3];
gets(a);
return 0;
}
Console in CLion won't print line after scaning input, but in iTerm after compiling it all works perfectly.
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
char personName;
printf("Hello, what is your name?\n");
scanf("%s", personName);
printf("Hello, %s\n", personName);
return 0;
}
I just got this in CLion console:
Hello, what is your name?
Mike
Process finished with exit code 11
You need to use an array for personName. The code will be,
#include <stdio.h>
#include <stdlib.h>
int main() {
char personName[32];
printf("Hello, what is your name?\n");
if(scanf("%s", personName))
printf("Hello, %s\n", personName);
return 0;
}
Here is my code. Substitution Cipher in C. But i got an error this line: char *encryption (char cipher_text[]) { function definition is not allowed here. I think probably "main" function place not right. How can i fix it?
And by the way how can i generate random alphabet for this code? Thank you so much.
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <string.h>
char *encryption (char cipher_text[]) {
int i, val, j;
printf("\n abcdefghijklmnopqrstuvwxyz \n");
You cannot define a function inside another one. encryption is defined in main :/
In C, you cannot declare a function inside another function, like you did.
Here is your code that will compile:
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <ctype.h>
#include <string.h>
char *encryption (char []);
void *decryption (char []);
char alpha [26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
char key[26];
char *encryption (char cipher_text[]) {
int i, val, j;
printf("enter the unique KEY of 26 character:");
scanf("%s", key);
printf("\n abcdefghijklmnopqrstuvwxyz \n");
printf ("%s", key);
for (i=0; i <strlen(cipher_text); i++)
{
for (j=0; j<26; j++)
{
if (alpha[j]== cipher_text[i]) {
cipher_text[i]=key[j];
break;
}
}
}
printf ("your message enc: %s", cipher_text);
return cipher_text;
}
int main ()
{
int i, key, choice, flag=0;
char *c_text, msg[255];
printf("\n Enter plain text:");
scanf ("%[^\n]", msg);
encryption(msg);
return 0;
}
How to generate random characters is answered here.
I want to display a full name, but I can't enter more than two parts of a name. The program stuck when enter a name which has more characters than the number which array has. How can I solve this?
#include <stdio.h>
#include<stdlib.h>
int main(){
char x[25];
printf("Enter your name");
scanf("%s",x);
printf("Your name is %s", x);
return 0;
}
Thank You
I think this can help you. This program doesnt care how many characters, spaces you entered. It only displays first 24 characters and spaces. (1 for string terminator)
#include <stdio.h>
#include <stdlib.h>
int main(){
char x[25];
char *xx=x;
puts("Input Name");
fgets(xx,25,stdin);
puts(xx);
return 0;
}
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();
}