Thread: Appending characters

  1. #1
    Registered User
    Join Date
    Jan 2019
    Posts
    1

    Appending characters

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

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,629
    Code:
    #include <stdio.h>
    #include <string.h>
      
    int main() {
        char str[200] = {0};
        int len = 0;
     
        while (1) {
            int choice;
            printf("\n1) Add char\n2) Empty all\n3) Print\n0) Stop\n");
            printf("Enter your choice: ");
            scanf("%d", &choice);
          
            if (choice == 1) { // add a letter to the string
                char ch;
                printf("Enter a letter: ");
                scanf(" %c", &ch); 
                str[len++] = ch;
                str[len] = '\0';
            }     
            else if (choice == 2) { // clear the string
                len = 0;
                str[0] = '\0';
            }     
            else if (choice == 3) // print what's in the string
                printf("%s\n", str);
            else if (choice == 0)
                break;
            else
                printf("Faulty input! Try again.\n");
        }
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem appending
    By Furious5k in forum C++ Programming
    Replies: 47
    Last Post: 01-06-2009, 08:40 AM
  2. Appending a list!
    By GCNDoug in forum C++ Programming
    Replies: 14
    Last Post: 10-06-2008, 02:19 PM
  3. Appending Strings
    By Fuyuki in forum C++ Programming
    Replies: 4
    Last Post: 11-17-2005, 11:38 AM
  4. Appending HTML to C .. help!
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 02-12-2005, 01:21 PM
  5. Appending strings
    By Ajsan in forum C++ Programming
    Replies: 3
    Last Post: 02-23-2004, 12:56 PM

Tags for this Thread