-4

I am trying to store multiple strings with spaces in array.

#include<stdio.h>
#define L 30

void main(){

    puts("\t+----------------------+\n\t| Contacts Application |\n\t+----------------------+\n");

    int noc; // number of contacts

    printf(" How many contacts do you want to store: ");
    scanf("%d", &noc);

    char name[noc][L];
        int  number[noc];
        int  a, b; // for counters in loop in switch

    if(noc>0){

        for(int i = 0; i < noc; i++){
            printf("\n\tName %d: ", i+1);
            //scanf("%s", &name[i]);
            fgets(name[i], L, stdin);

            printf("\tNumber: ");
            scanf("%d", &number[i]);
        }

        /** This will clear the screen **/
        #ifdef _WIN64
         system("cls");
        #elif __linux__
         system("clear");
        #endif

        puts("\n\tAll contacts have been saved successfully.\n");
        puts("\t1. Show all the contacts.");
        puts("\t2. Search any contact.\n");

        int choice;
        printf("\tEnter your choice: ");
        scanf("%d", &choice);

        switch(choice){
            case 1:

             for(int a = 0; a < noc; a++){
                printf("\n\t %d: %s - %d", a+1, name[a], number[a]);
             }

             break;

            case 2:
             //searchContact();

            default:
             puts("\n\tInvalid option, please try again.\n");
        }

    } else{
        puts("\nPlease enter more than zero.");
    }
} // main function

And this code doesn't work, don't know why, @kutt in the comments added an example, that works but this does not, why?

What can be done to resolve the issue as the execution directly passes to the &number's scanf().

  • 2
    Use fgets instead – klutt Jan 22 '20 at 15:23
  • Does this help? https://stackoverflow.com/q/58403537/6699433 – klutt Jan 22 '20 at 15:24
  • the array is two dimensional so gets(),, fgets() don't work. – Takshak Rajput Jan 22 '20 at 15:27
  • 1
    Yes, fgets works perfectly fine – klutt Jan 22 '20 at 15:29
  • Here is the code, let me know what's wrong: https://onlinegdb.com/By__okUZ8 – Takshak Rajput Jan 22 '20 at 15:35
  • https://onlinegdb.com/BkvchkUWI – klutt Jan 22 '20 at 15:39
  • This doesn't resolve the issue at https://onlinegdb.com/By__okUZ8 – Takshak Rajput Jan 22 '20 at 15:50
  • @TakshakRajput You should [edit] your question and add all information and clarification there instead of answering in comments. You should also add your full source code to the question. (in addition to a link to onlinegdb) Please explain in your question what issue you have with the linked code. – Bodo Jan 22 '20 at 15:50
  • Well, you have not told me what the issue is. You said that `fgets` don't work with 2-dimensional arrays, and I proved that it does. – klutt Jan 22 '20 at 15:51
  • You should post a question containing a [mre] with expected and actual behavior. – klutt Jan 22 '20 at 15:53
  • 1
    Sorry for being harsh, but you have been around here for a while now and "It does not work" is not a problem description. I don't know what your input or output is, nor what you expect instead, and it's not up to us to figure it out. That's something that should be clear in the question. Furthermore, you should also remove all code that's not necessary to reproduce the problem. – klutt Jan 22 '20 at 16:01

1 Answers1

1

You don’ have to use fgets if you add a space in front of %d and %[^\n]. You should also test the return value of scanf to check if the operation was successful.

Here is the fixed code.

If this code I repeat the request for a number if the input value is not a number.

#include<stdio.h>
#include <stdlib.h>

void main(){

    puts("\t+----------------------+\n\t| Contacts Application |\n\t+----------------------+\n");
    int noc; // number of contacts
    int cnt;
    do {
        printf(" How many contacts do you want to store: ");
        cnt = scanf("%d", &noc);
    } while (cnt != 1);


    if(noc>0){
        char name[noc][30];
        int  number[noc];
        int  a, b; // for counters in loop in switch

        for(int i = 0; i < noc; i++){
            printf("\n\tName %d: ", i+1);
            scanf(" %[^\n]", &name[i]);

            do {
                printf("\tNumber: ");
                cnt = scanf(" %d", &number[i]);
            } while (cnt != 1);
        }

        /** This will clear the screen **/
        #ifdef _WIN64
         system("cls");
        #elif __linux__
         system("clear");
        #endif

        puts("\n\tAll contacts have been saved successfully.\n");
        puts("\t1. Show all the contacts.");
        puts("\t2. Search any contact.\n");

        int choice;
        printf("\tEnter your choice: ");
        scanf("%d", &choice);

        switch(choice){
            case 1:

             for(int a = 0; a < noc; a++){
                printf("\n\t %d: %s - %d", a+1, name[a], number[a]);
             }

             break;

            case 2:
             //searchContact();

            default:
             puts("\n\tInvalid option, please try again.\n");
        }

    } else{
        puts("\nPlease enter more than zero.");
    }
} // main function
chmike
  • 17,648
  • 19
  • 66
  • 93