Question

Need help, i can't get a infinite loop to work, that terminates when a blank line...

Need help, i can't get a infinite loop to work, that terminates when a blank line is inputted for the firstName.

note: C programing langauge and using microsoft visual studios .

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <stdbool.h>
int main() {

   //int arraySalaries[50];
   int calculate_avg = 0;
   //int calculate_max;
   //int calculate_min;
   int salary[50];
   //int length;
   //int average_salary;
   char firstName[50][50];
   char lastName[50][50];
   int i = 0;
   int j = 0;

   while (i < 50 && j < 50) {
       printf("Enter first Name:\n");
       fgets(firstName[j], sizeof firstName[j], stdin);

       if ( sizeof firstName[j][0] == NULL) {
           break;
       }
       if (firstName[j][0] == '\n') {
           break;
       }
       firstName[j][strlen(firstName) - 1] = '\0';
      
       printf("Enter last name: ");
       scanf("%s", lastName[i]);
      
       printf("Enter salary: ");
       scanf("%d", &salary[i]);
       i++;
       j++;
   }
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#define _CRT_SECURE_NO_WARNINGS

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <ctype.h>

#include <math.h>

#include <stdbool.h>

int main() {

       //int arraySalaries[50];

          int calculate_avg = 0;

          //int calculate_max;

          //int calculate_min;

          int salary[50];

          //int length;

          //int average_salary;

          char firstName[50][50];

          char lastName[50][50];

          int i = 0;

       // int j = 0; // no need of j

          while (i < 50 ) {

                printf("Enter first Name:\n");

                fgets(firstName[i], sizeof(firstName[i]), stdin); // fgets might add a newline at the end of the string read, this makes the length of empty string as 2

                if(firstName[i][1] == '\n') // check if character at index 1 = newline, then empty string

                       break;

                //firstName[i][strlen(firstName[i]) - 1] = '\0';

                printf("Enter last name: ");

                scanf("%s", lastName[i]);

                printf("Enter salary: ");

                scanf("%d", &salary[i]);

            

                i++;

          }

}

Add a comment
Know the answer?
Add Answer to:
Need help, i can't get a infinite loop to work, that terminates when a blank line...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char...

    C program help #include #include #include void PrintName(char firstname[16], char lastname[16]); int main () { char firstname[16]; char lastname[16]; printf("please enter your first name:"); scanf("%s",firstname); printf("please enter your last name:"); scanf("%s",lastname); PrintName(firstname,lastname); return 0; } void PrintName(char firstname[16], char lastname[16]){ char fullname[34]; *fullname=*firstname+*lastname; (char*) malloc (sizeof (*fullname)); if (strlen(firstname) > 16||strlen(lastname)>16||strlen(fullname)>16) fflush(stdin); else printf(" the full name is %s %s \n",firstname,lastname); printf(" the full name is-> %s",fullname);/*why is one not run*/ return 0; }

  • Please help modify my C program to be able to answer these questions, it seems the...

    Please help modify my C program to be able to answer these questions, it seems the spacing and some functions arn't working as planeed. Please do NOT copy and paste other work as the answer, I need my source code to be modified. Source code: #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { char title[50]; char col1[50]; char col2[50]; int point[50]; char names[50][50]; printf("Enter a title for the data:\n"); fgets (title, 50, stdin); printf("You entered: %s\n", title);...

  • Help with my code: The code is suppose to read a text file and when u...

    Help with my code: The code is suppose to read a text file and when u enter the winning lotto number it suppose to show the winner without the user typing in the other text file please help cause when i enter the number the code just end without displaying the other text file #include <stdio.h> #include <stdlib.h> typedef struct KnightsBallLottoPlayer{ char firstName[20]; char lastName[20]; int numbers[6]; }KBLottoPlayer; int main(){ //Declare Variables FILE *fp; int i,j,n,k; fp = fopen("KnightsBall.in","r"); //...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program...

    Here is the (A3b-smallgrades.txt) text file: Here is the (A3b-largegrades.txt) text file: Here is the Program A3a without modification: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char students[COLS][20]); void printGrades(int ROWS, int COLS, int grades[ROWS][COLS]); void getStudents(int COLS, char students[COLS][20]); void printStudents(int COLS, char students[COLS][20]); void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char Fgrades[]); void printFinalGrades(int COLS, char Fgrades[]); int main() {    srand(time(0));    int stu = 0, assign = 0;...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • Can someone help me fix my C code. I am getting segmentation fault along with missing...

    Can someone help me fix my C code. I am getting segmentation fault along with missing termination " /* These are the included libraries. */ #include #include #include #include void printTriangle(char *str); int main() { char sentence[81]; int done = 0; while(done != 1) { printf("Please enter the sentence or quit: "); fgets(sentence, 80, stdin); if(strcmp(sentence, "quit") == 0) done = 1; else printTriangle(sentence); } } void printTriangle(char *str) { int index = 0; int line = 1; int i;...

  • #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here....

    #include <stdio.h> #include <string.h> #include <ctype.h> #include <stdlib.h> int main(void) { /* Type your code here. */ int GetNumOfNonWSCharacters(const char usrStr[]) { int length; int i; int count = 0; char c; length=strlen(usrStr); for (i = 0; i < length; i++) { c=usrStr[i]; if ( c!=' ' ) { count++; } }    return count; } int GetNumOfWords(const char usrStr[]) { int counted = 0; // result // state: const char* it = usrStr; int inword = 0; do switch(*it)...

  • You must use C Language. The Main Objective: Make the first and last name ALL CAPITALS...

    You must use C Language. The Main Objective: Make the first and last name ALL CAPITALS even if the user types in lower case letters. Lastly, flip the first name and last name around. So: HEIDI, HATFIELD to HATFIELD, HEIDI. (PLEASE uppercase all the letters) End Goal: HATFIELD, HEIDI KAISER, RUSSELL LIPSHUTZ, HOWARD PENKERT, DAWN WRIGHT, ELIZABETH Please use this reference below, to fix the code given. Code given is below, I want the user to be able to enter...

  • I am trying to add a string command to my code. what am i doing wrong?...

    I am trying to add a string command to my code. what am i doing wrong? #define _CRT_SECURE_NO_WARNINGS #define _USE_MATH_DEFINES #include <stdio.h> #include <string.h> #include <math.h> int main(void) {    int i, j;    int rowA, colA, rowB, colB;    int A[10][10], B[10][10];    int sum[10][10];    char str1[10];    printf("This is a matrix calculator\n");    //read in size from user MATRIX A    printf("Enter in matrix A....\n");    printf("\t#row = ");    scanf("%d", &rowA);    printf("\t#col = ");   ...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT