CLion won't print in console after scanf - c

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;
}

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);
}

tcc: undefined symbol '_MessageBoxA#16' error on my c code

I tried to include <windows.h> with also <stdio.h> but it shows: tcc: undefined symbol '_MessageBoxA#16' when i use my only Pocket Edition TinyCCompiler. does anybody know why?
#include <stdio.h>
#include <winapi/windows.h>
#include <winapi/winuser.h>
/* ooops fell of my chair */
int main()
{
char name[40];
char age[6];
printf("Enter name:");
scanf("%39s", name);
printf("\n");
printf("Enter age:");
scanf("%5s", age);
printf("\n");
printf("You: Hello my name is %s, and i'm %s years old.",name,age);
printf("\n----------------OTHER PROGRAM----------");
MessageBox(0, "Hello", "Hellodd", 1);
return 0;
}
You need to link with user32:
tcc -Wall code.c -luser32

Print a string till a particular character comes

I want the string to be printed till character ('e') comes.
Code which I tried:-
#include <stdio.h>
int main() {
int a,i,x;
char b[10];
char ch;
//enter input string
for(i=0;i<10;i++)
scanf("%c",&b[i]);
for(i=0;i<10;i++)
if(b[i]!='e')
printf("%c",b[i]);
return 0;
}
Input:abcdefghij
Actual output:abcdfghij
Desired output:abcd
Question : Where am I wrong ? Will putting a break inside if block work here?
This is much cleaner if you want to use scanf.
#include <stdio.h>
int main()
{
char b[101];
scanf("%100s", b);
printf("%s\n", b);
return(0);
}
Or even better.
#include <stdio.h>
#define MAX_LENGTH 100
int main()
{
char b[MAX_LENGTH+1]; // add 1 for the terminating zero
scanf("%100s", b);
printf("%s\n", b);
return(0);
}
This one uses fgets to read the entire line.
#include <stdio.h>
#define MAX_LENGTH 100
int main()
{
char b[MAX_LENGTH];
fgets(b, MAX_LENGTH, stdin);
printf("%s", b);
return(0);
}
How to print a string till limit?
What code should do is use fgets().
Avoid using scanf(). Is is too easy to use wrong.
#include <stdio.h>
#include <string.h>
int main() {
char b[100];
if (fgets(b, sizeof b, stdin)) {
// If code needs to lop off the potential \n at the end
b[strcspn(b, "\n")] = '\0';
printf("%s\n", b);
}
return 0;
}
Advanced issues include how to handle excessively long input lines and error handling - not shown here.
Here is what you need to do
#include <stdio.h>
int main()
{
int a,i,x;
char b[10];
char ch;
//enter input string
for(i=0;i<10;i++)
{
scanf("%c",&b[i]);
}
for(i=0;i<10;i++)
{
if(b[i]=='e')
{
break;
}
}
return 0;
}
re
There are several mistakes!
If you are initializing your loops from 0 then you need to set the condition till i<100.
Change your format specifiers to %s.
Change your IF statement to if(b[i]!='\0').
#include <stdio.h>
int main()
{
int i;
char b[10];
for(i=0;i<10;i++)
{
scanf("%c",&b[i]);
}
for(i=0;i<10;i++)
{
if(b[i]=='e')
{
break;
}
printf("%c",b[i]);
}
return 0;
}

Mono alphabetic cipher doesnt work correctly

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.

c isalpha and isdigit while loop

How do I put isalpha and isdigit in a while(1) loop?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
int i;
char type[256];
printf("You can type a number or a word. Type exit to exit! \n");
printf("Type: ");
fgets (type, 256, stdin);
if (isalpha(type[i]))
{
printf("Typed text: %s\n", type);
if((strcmp(type,"exit\n") == 0))
{
printf("Exiting...\n");
exit(1);
}
}
else if (isdigit(type[i]))
{
printf("Typed number: %s\n", type);
}
else
{
printf("Typed: %s\n", type);
printf("Its not a letter or number...?!\n");
}
}
I tried adding while(1) at the start at the code and close it at the end of code, but as soon as I enter number or letter the console crashes... Could someone please help me with this?
Your problem is not a loop problem, you need to give a value to i , as it is undefined and you get a nice crash. Please replace
int i;
with
int i=0;

Resources