Hello!
The idea of my program is to ask each time for a character, to save it so I can add a new character that will be added after the first one etc. Then there's a choice to empty it or print it all.
So a example how it should work:
1) Input: H
*Choose 1) again*
1) Input: i
*Choose 3)*
it should print: Hi
It's not working how I want it to tho, I'd appreciate some help on what to do. Thank you!
Code:
#include <stdio.h>
#include <string.h>
int main(void) {
int choice;
char str[20] = {0};
char str1;
while(1) {
printf("Menu\n1) Add char\n2)Empty all\n3)Print\n0)Stop\n");
printf("Give your choice: "); //Asks the choice from the menu
scanf("%i", &choice);
if(choice == 1) { //This choice adds a letter to the (empty) string
printf("Give a letter: ");
scanf(" %c", &str1);
int len = strlen(str);
str[len] = str1;
str[len + 1] = '\0'; }
else if(choice == 2) { //choice 2 clears the string
printf("");
scanf("%s", str1); }
else if(choice == 3) { //choice 3 prints what's in the string
printf("%s\n", str1); }
else if(choice == 0) {
printf("Wrong input!");
break; }
else { //if choice is wrong, it ends the program
printf("Faulty input!\n");
break; }
} }