/*
Program to calculate trip and plan flights
*/
#define TRIP 6
#define NAMEMAX 40
#define DEST 1
#include <stdio.h>
#include <string.h>
int main(void)
{
int i, trip_num, row, col;
char travel_name[TRIP][DEST],
dest_in[1];
printf("Please enter the number of trips:");
scanf("%d", &trip_num);
while (trip_num > TRIP)
{
printf("Invalid number of trip. (Min of 3 trips and Max 6 trips).\n");\
/*input number of trips*/
printf("Please enter the number of trips:");
scanf("%d", &trip_num);
if (trip_num < TRIP)
printf("Valid trip number. Please proceed to enter destination code.\
\n");
}
for (i=0; i < trip_num ; i++)
{
printf("Please enter name of destination(Note: use _ to for spaces in \
name):\n");
scanf("%s", &dest_in);
if (strlen(dest_in) < NAMEMAX)
strcpy(travel_name[i],dest_in);
/* else (strlen(dest_in) > NAMEMAX) */
for (row = 0; row < trip_num; row++)
{
for (col=0; col < DEST; col++)
printf("Trip#:%d travel_name:%s \n", row+1, travel_name[row][col]);
}
return 0;
}
我試圖讓用戶把一個名稱爲字符串,並將其存儲如果名稱是40個字符以內,但它給了我一個分段錯誤字符串/分段故障
> travel_name [i] [0] = dest_in [100]; < - 不知道這裏有什麼意圖,但是dest_in [100]不是有效的索引。 – Kevin 2011-02-25 20:10:25