You have a print loop inside your read loop. He tries to print all the trip information after reading the first one.
Edit: The problem is that the scanf method handles individual characters, is not intuitive next to how it handles strings and numbers. It reads the next character from the standard, which is probably a newline when you finish typing the previous input. He then tries to try to read the integer, but instead finds the letter you intended to use, %c . This causes a scanf error and does not initialize stop_num .
In one case, this can be read in a line. scanf will start reading a line in the first character without spaces and will stop reading it with the first space character. Then just grab the first character from the buffer into which you are reading the line.
#include <stdio.h> #define MAX 3 #define MAXTRIP 6 struct stop { float cost; float time; }; struct trip { char Dest_letter; int stop_num; struct stop leg[MAX]; }; int main(void) { int trip_num, index, i; struct trip travel[MAXTRIP]; char buffer[10]; printf("Enter number of trips: "); scanf("%d", &trip_num); for (index = 0; index < trip_num; index++) { printf("Please enter destination letter/number of stops:\n"); scanf("%s %d", buffer, &travel[index].stop_num); travel[index].Dest_letter = buffer[0]; for (i = 0; i < travel[index].stop_num; i++){ printf("Please enter cost/length of stop %d:\n", i); scanf("%f %f", &travel[index].leg[i].cost, &travel[index].leg[i].time); } } printf("%d trips\n", trip_num); for (index = 0; index < trip_num; index++) { printf("Trip:%d \nDestination Letter:%c\n", index + 1, travel[index].Dest_letter); for (i = 0; i < travel[index].stop_num; i++) printf("Cost:%.2f \nLength:%.2f\n", travel[index].leg[i].cost, travel[index].leg[i].time); } }
source share