如果没有为名字输入值,如何进行循环中断

时间:2017-04-09 21:40:11

标签: c loops for-loop if-statement break

我无法让我的代码以第一个名字打破,它只会在我将所有值留空后才会中断。

class TennisSearchResultsContainer extends React.Component {
  componentWillMount () {
    // TODO: Build markers for the map
    // TODO: Check courtsResults object and database for tennis court
    this.courtsMarkers = this.props.courtsResults.map((court) => {
      return new google.maps.Marker({
        position: new google.maps.LatLng(JSON.parse(court.LOC).coordinates[1], JSON.parse(court.LOC).coordinates[0]),
        title: court.NAME,
        animation: google.maps.Animation.DROP
      });
    });
  }
  render () {
    return <TennisSearchResults criterias={this.props.criterias} courtsMarkers={this.courtsMarkers} />;
  }
}

这是输出。

#include <stdio.h>
#include <stdbool.h>
#include <string.h>


void main(void) { //Start main function

    // Declare variables
    char charTempArray[50] = "";
    char charFirstNames[50][50];
    char charLastNames[50][50];
    char charTempSal[10] = "";
    int intSalaries[50];
    int intSalarySum = 0;
    int intSalaryAvg = 0;
    int intSalaryTop = 0;
    int intSalaryBot = 9999999999;
    int intArraySize = 0;
    int i = 0;
    int intCharConv = 0;

    //User input to build the arrays
    for(i = 0; i < 50; ++i)
        if (charFirstNames[i - 1][0] != '\0') {
            printf("Please enter Employees first name.\n ");
            gets(charTempArray);
            strcpy(charFirstNames[i], charTempArray);
            printf("Please enter Employees last name.\n ");
            gets(charTempArray);
            strcpy(charLastNames[i], charTempArray);
            printf("Please enter Employees salary.\n ");
            gets(charTempSal);
            intCharConv = atoi(charTempSal);
            intSalaries[i] = intCharConv;
            intArraySize = i;
        }
        else {
            break;
        }

1 个答案:

答案 0 :(得分:1)

首先,当if时,您的i=0将尝试访问数组范围之外的元素。第二,你应该在获得第一个名字后立即测试空名:

for(i = 0; i < 50; ++i)
{
    printf("Please enter Employees first name.\n ");
    gets(charTempArray);
    strcpy(charFirstNames[i], charTempArray);
    if(charTempArray[0] == 0)
        break;
    printf("Please enter Employees last name.\n ");
    gets(charTempArray);
    strcpy(charLastNames[i], charTempArray);
    printf("Please enter Employees salary.\n ");
    gets(charTempSal);
    intCharConv = atoi(charTempSal);
    intSalaries[i] = intCharConv;
    intArraySize = i;
}