Member 13033236 Ответов: 2

Я получаю комментарий " id вернул 1 статус выхода. Что я сделал не так


#include <stdio.h>
#include <stdlib.h> // remember if you plan to use exit(), you need
					// this header file
 


main() 
{
	// Despite being a long program, you only need two variables:
	// one for the first menu and one for the second menu
	
	int choice1;
	int choice2;
	
	// The potential decade choices
	
	printf("What do you want to see?\n");
	printf("1. The 1980's\n");
	printf("2. the 1990's\n");
	printf("3. The 2000's\n");
	printf("4. Quit\n");
	
	//The top menu choice and the switch statement that makes the
	// resulting
	//information appear are encased in a do-while loop that
	//ensures one
	// of the four menu choices are made
	do
	{
		
		printf("Enter your choice: ");
		scanf(" %d", &choice1);
		switch (choice1)
		{
			//In the first case, the user picked the 1980's. Now it
			//time to see what specific info they need.
			
			case (1) : 
			{
				printf("\nWhat would you like to see?\n");
				printf("1. Baseball\n");
				printf("2. The movies\n");
				printf("3. US Presidents\n");
				printf("4. Quit\n");
				
				
				printf("Enter your choice: ");
				scanf(" %d", &choice2);
				
				if (choice2 ==1)
				{
					printf("\n\nWorld Series Chapions ");
					printf(" of the 1980's:\n");
					printf("1980: Philadelphia Phillies\n");
					printf("1981: Los Angeles Dodgers\n");
					printf("1982: St. Louis Cardinals\n");
					printf("1983: Baltimore Orioels\n");
					printf("1984: Detroit Tigers\n");
					printf("1985: Kansas City Royals\n");
					printf("1986: New York Mets\n");
					printf("1987: Minnesota Twins\n");
					printf("1988: Los Angeles Dodgers\n");
					printf(" 1989 Oakland A's\n");
					printf("\n\n\n");
					break;
					
				} else if (choice2 == 2)
				{
					printf("\n\nOscar winning movies in the 1980's:\n");
					printf("1980: Ordinary People\n");
					printf("1981: Chariots of Fire\n");
					printf("1982: Ghandi\n");
					printf("1983: Terms of Endearment\n");
					printf("1984: Amadeus\n");
					printf("1985: Out of Africa\n");
					printf("1986: Platoon\n");
					printf("1987: The Last Emperor\n");
					printf("1988: Rain man\n");
					printf("1989: Driving Miss Daisy");
					printf("\n\n\n");
					break;
				} else if (choice2 == 3)
				{
					printf("\n\n US Presidents in the 1980's:\n");
					printf("1980: Jimmy Carter\n");
					printf(" 1981-1988: Ronald Reagen\n");
					printf("1989: George Bush\n");
					printf("\n\n\n");
					break;
					
				} else if (choice2 == 4)
				{
					exit(1);
				} else
				{
					printf("Sorry, that is not a valid choice!\n");
					break;
				}
			}
			
			//This is the case for the 1990's.
			// Unlike the top menu, there isn't a data validation
			//do-while loop
			
			case (2) :
				{
					printf("\n\n What would you like to see?\n");
					printf("1. Baseball\n");
					printf("2. The movies\n");
					printf("3. US Presidients\n");
					printf("4. Quit\n");
					
					printf(" Enter you choice:");
					scanf(" %d", &choice2);
					
					if (choice2 ==1)
					{
						printf("\n\n World Series Champions of");
						printf("the 1990's:\n");
						printf("1990: Cincinatti reds\n");
						printf("1991: Minnesota Twins\n");
						printf("1992: Toronto Blue jays\n");
						Printf("1993: Toronto Blue Jays\n");
						printf("1994: No World Series\n");
						printf("1995: Atlanta Braves\n");
						printf("1996: New York Yankees\n");
						printf("1997: Florida Marlins\n");
						printf("1998: New York Yankees\n");
						printf("1999 New York Yankess\n");
						printf("\n\n\n");
						break;
						
					} else if (choice2 == 2)
					{
						printf("\n\n Oscar winning Movies in ");
						printf(" the 1990's:\n");
						printf("1990: Dances with Wolves\n");
						printf("1991: The Silence of the Lambs\n");
						printf("1992: Unforgiven\n");
						printf("1993: Schindlers List\n");
						printf("1994: ?\n");
						printf("1995: ??\n");
						printf("1996: The English Patient\n");
						printf("1997: Titanic\n");
						printf("1998: Shakespere in Love\n");
						printf("1999: American Beauty\n");
						printf("\n\n\n");
						break;
					} else if (choice2 == 3)
					{
						printf("\n\nUS Presidents in the 1990's:\n");
						printf("1990-1992: George Bush\n");
						printf("1993-1999: Bill Clinton\n");
						printf("\n\n\n");
						break;
					} else if (choice2 == 4)
					{
						exit (1);
					} else
					{
						printf("Sorry, that is not a valid choice!\n");
						break;
					}
				}
				
				// The section for when the user selects the 2000's
				case (3) :
					{
						printf("\n\nWhat would you like to see?\n");
						printf("1. Baseball\n");
						printf("2. The Movies\n");
						printf("3. US Presidents\n");
						printf("4. Quit\n");
					
						printf("Enter your choice:");
						scanf(" %d", &choice2);
						
						if (choice2 == 1)
						{
							printf("\n\nWorld series Champions of ");
							printf("The 2000's:\n");
							printf("2000: New York Yankees\n");
							printf("2001: Arizona Diamondbacks\n");
							printf("2002: Anaheim Angels\n");
							printf("2003: Florida Marlins\n");
							printf("2004: Boston Red Sox\n");
							printf("2005: Chicago White Sox");
							printf("2006: St. Louis Cardinals\n");
							printf("2007: Boston Red Sox\n");
							printf("2008: Philadelphia Phillies\n");
							printf("2009: New York Yankees\n");
							printf("\n\n\n");
							break;
						} else if (choice2 == 2)
						{
							printf("\n\nOscar winning movies in ");
							printf("the 2000's: \n");
							printf("2000: Gladiator\n");
							printf("2001: A Beautiful Mind\n");
							printf("2002: Chicago\n");
							printf("2003: The Lord of the Rings\n");
							printf("2004: Million Dollar Baby\n");
							printf("2005: Crash\n");
							printf("2006: The Departed\n");
							printf("2007: No Country for Old Men\n");
							printf("2008: Slumdog Millionaire\n");
							printf("2009: The Hurt Locker\n");
							printf("\n\n\n");
							break;
						} else if (choice2 == 3)
						{   
							printf("\n\nUS Presidents in the 2000's\n");
							printf("2000: Bill Clinton\n");
							printf("2001-2008: George W Bush\n");
							printf("2009: Barack Obama\n");
							printf("\n\n\n");
							break;
						} else if (choice2 == 4)
						{	
							exit(1);
							
						} else
						{
							printf("Sorry, that is not a valid choice!\n");
							break;
						}
		}
		case (4) :
			exit (1);
			default : printf("\n%d is not a valid choice.\n");
				printf("Try again.\n");
				break;
	}
} while ((choice1 < 1) || (choice1 > 4));

	return 0;
}


Что я уже пробовал:

Я пробовал просмотреть программу, но ничего очевидного в его появлении не было.

Graeme_Grant

Здорово видеть форматирование, именованные переменные и комментарии к коду...

Однако это очень много повторяющегося кода. Почему бы не использовать массивы для хранения вопросов, а затем передать их функции? Таким образом, он упрощает код и делает отладку намного проще...

2 Ответов

Рейтинг:
1

Patrice T

Когда вы не понимаете, что делает ваш код или почему он делает то, что делает, ответ таков: отладчик.
Используйте отладчик, чтобы увидеть, что делает ваш код. Просто установите точку останова и посмотрите, как работает ваш код, отладчик позволяет вам выполнять строки 1 на 1 и проверять переменные по мере их выполнения, это невероятный инструмент обучения.

Отладчик-Википедия, свободная энциклопедия[^]
Освоение отладки в Visual Studio 2010 - руководство для начинающих[^]

Отладчик здесь для того, чтобы показать вам, что делает ваш код, и ваша задача-сравнить его с тем, что он должен делать.
В отладчике нет никакой магии, он не находит ошибок, он просто помогает вам. Когда код не делает того, что ожидается, вы близки к ошибке.


Рейтинг:
0

Graeme_Grant

Вы установили точки останова, чтобы поймать поток вашего кода? Отладчик может очень помочь вам и сэкономить кучу времени.

Я вижу ошибку, которую нужно исправить:

default : printf("\n%d is not a valid choice.\n");
должно быть:
default : printf("\n%d is not a valid choice.\n", choice1);