473,323 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,323 software developers and data experts.

How do I make a program so that the user inputs the amount of strings he wants to ent

Right now I am just trying to get my program to receive input from the user of how many strings he/she wants to enter and then he/she will enter those strings. This is what I have, but when I try to run the program the compiler crashes. I am not sure how to do this another way without the compiler crashing and I don't understand why it's crashing. This is for a project I am working on in my programming class so I cannot change what the user can input.


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define NUM_MSG 10
  5. #define MSG_LENGTH 60
  6.  
  7.  
  8. void read_line (char str[], int n)
  9. {
  10.     int i = 0;
  11.     char ch;
  12.     while ((ch = getchar()) != '\n')
  13.     {
  14.         if (i < n)
  15.         {
  16.             str[i++] = ch;
  17.         }
  18.     }
  19.  
  20.     str[i] = '\0';
  21.  
  22. }
  23.  
  24. int main()
  25. {
  26.     char reminders[NUM_MSG][MSG_LENGTH+1];
  27.  
  28.     int counter = 0;
  29.     int n;
  30.  
  31.     scanf("%d", n) //here is where the user enters how many strings he/she wants
  32.  
  33.     while (1)
  34.     {
  35.  
  36.         read_line(reminders[counter], MSG_LENGTH);
  37.  
  38.         counter++;
  39.  
  40.         if (counter > n) //i want to end the loop when the user enters n amount of strings
  41.         {
  42.             break;
  43.         }
  44.     }
  45.  
  46.     return 0;
  47. }
  48.  
Mar 29 '15 #1

✓ answered by donbock

What do you mean by "the compiler crashes"?
  • Does the compiler truly crash?
  • Does the compile fail with errors or warnings?
  • Does the compile succeed, but the program fails with a runtime error?
In any case, it would be helpful if you provided the precise text of the error message(s).

A few observations:
  • getchar returns an int, not a char.
  • The read_line loop should also terminate if getchar returns EOF or '\0'.
  • You should verify that the value entered for n is between 1 and 10.
  • The array indices for a n-element array range from 0 to n-1; thus, the loop in main should terminate when counter >= n.

1 1251
donbock
2,426 Expert 2GB
What do you mean by "the compiler crashes"?
  • Does the compiler truly crash?
  • Does the compile fail with errors or warnings?
  • Does the compile succeed, but the program fails with a runtime error?
In any case, it would be helpful if you provided the precise text of the error message(s).

A few observations:
  • getchar returns an int, not a char.
  • The read_line loop should also terminate if getchar returns EOF or '\0'.
  • You should verify that the value entered for n is between 1 and 10.
  • The array indices for a n-element array range from 0 to n-1; thus, the loop in main should terminate when counter >= n.
Mar 30 '15 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: Sven Neuberg | last post by:
Hi, I have been handed the task of updating and maintaining a web application, written in ASP and Javascript, that takes complex user inputs in HTML form and submits them to server-side ASP...
10
by: jeff regoord | last post by:
A user inputs a float value. The scanf() function gets the value. However, I need to create an error handler with an if else statement saying invalid input if the input is not a number. Does...
1
by: Scott | last post by:
Hi, I'm using a form to collect values for later analysis. My question is, how do I validate the value the user inputs into a textbox? For example, many of the values require a real number and...
25
by: sravishnu | last post by:
Hello, I have written a program to concatanae two strings, and should be returned to the main program. Iam enclosing the code, please give me ur critics. Thanks, main() { char s1,s2;...
6
by: jrshack | last post by:
I have a web form that users input answers and when submitted I want the ASP code to place the user inputs into certain locations of a HTML form. The form is then automatically opened in a new...
1
by: Dinu | last post by:
How to Dynamically program User controls in a Master page from a content page Thanks
3
by: malika | last post by:
How to create dynamically tables on the basis of user inputs in the textbox for example if user write 3 in the textbox three table should be display n so on code of table is this is my table code...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.