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. -
#include <stdio.h>
-
#include <string.h>
-
-
#define NUM_MSG 10
-
#define MSG_LENGTH 60
-
-
-
void read_line (char str[], int n)
-
{
-
int i = 0;
-
char ch;
-
while ((ch = getchar()) != '\n')
-
{
-
if (i < n)
-
{
-
str[i++] = ch;
-
}
-
}
-
-
str[i] = '\0';
-
-
}
-
-
int main()
-
{
-
char reminders[NUM_MSG][MSG_LENGTH+1];
-
-
int counter = 0;
-
int n;
-
-
scanf("%d", n) //here is where the user enters how many strings he/she wants
-
-
while (1)
-
{
-
-
read_line(reminders[counter], MSG_LENGTH);
-
-
counter++;
-
-
if (counter > n) //i want to end the loop when the user enters n amount of strings
-
{
-
break;
-
}
-
}
-
-
return 0;
-
}
-
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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;...
|
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...
|
by: Dinu |
last post by:
How to Dynamically program User controls in a Master page from a
content page
Thanks
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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...
|
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...
| |