Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C Programming HOMEWORK HELP!!! I want to ask a user: \"How many sentences you wi

ID: 3730319 • Letter: C

Question

C Programming HOMEWORK HELP!!!

I want to ask a user: "How many sentences you wish to enter" but I want the max to be 10, or produce error if number is higher than 10 and try again

How do I do this? Can you help me?

-----------------------------------------------------------------------

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

int main(){

char **mySen;
int numSentences;

scanf("%d", &numSentences);

printf("You have put in %d.", numSentences);

mySen= (char **)malloc(numSentences*sizeof(char *));

int i;

for(i=0;i<numSentences; i++){

printf("Enter Sentence #%d: ",(i+1));

mySen[i] = (char *)malloc(100*sizeof(char));

scanf(" %[^ ]s", mySen[i]);

}

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>
void main()
{
char **mySen;
int numSentences;
int i;
clrscr();
a: // this is a label
printf(" How many sentences you wish to enter ");
scanf("%d", &numSentences);
if(numSentences>10 || numSentences<0) //your condition for max 10 sentences also gives error if user enters a negative number
{
printf("Maximum number of sentences you are allowed to enter is 10. Please try again!! ");
goto a; // this make the program execution to jump to the specified label
}
printf(" You have put in %d. ", numSentences);
mySen= (char **)malloc(numSentences*sizeof(char *));
for(i=0;i<numSentences; i++)
{
printf(" Enter Sentence %d: ",(i+1));
mySen[i] = (char *)malloc(100*sizeof(char));
scanf(" %[^ ]s", mySen[i]);
}
printf(" Program end. Press any key...");
getch();
}