I made a simple program where a user can pick from the menu. My problem is where do i put this code properly inside the fuction of funcFloat and funcInteger

if (myFloat < 0)  // this will output if the user entered a negative value	{
		printf ("You entered a negative value.\n\n");
	}
	else  // this will output if the user entered a positive value
	{
		printf ("You entered a positive value.\n\n");
	}

Here is the whole code i wrote:

#include "stdafx.h"
#include <stdio.h>

void load_menu(void);
void funcChar(void);
//void funcString(void);
void funcFloat(void);
void funcInteger(void);
//void funcDisplay(void);

int main(int argc, char** argv)
{
	load_menu();
	return 0;
}

void load_menu(void)
{
	int choice;

	do 
	{
		printf("\n\n**** MAIN MENU ****\n\n");
		printf("1. Enter a char\n");
	//	printf("2. Enter a string\n");
		printf("2. Enter a float\n");
		printf("3. Enter an integer\n");
	//	printf("5. Display the square of numbers from 1-100\n");
		printf("4. Quit the Program\n\n");

		printf("Enter your choice: ");
		scanf("%d",&choice);

		switch(choice)
		{
			case 1: funcChar();
				break;
		//	case 2: funcString();
		//		break;
			case 2: funcFloat();
				break;
			case 3: funcInteger();
				break;
		//	case 5: funcDisplay();
		//		break;
			case 4: printf("Quitting program!\n\n");
				break;
			default: printf("Invalid choice! Choose Again.\n");
				break;
		}

	} while (choice != 4);

}

void funcChar(void)
{
	char myChar;
	int ch;
	
	printf("\nEnter a letter: ");
	scanf("%c",&myChar);

	/* Flushes input buffer from the newline from scanf() */
	while ( (ch = getchar()) != '\n' && ch != EOF) ;

	printf("\nPress ENTER to return to MAIN MENU.");
	while ( (ch = getchar()) != '\n' && ch != EOF);

	return;
}		

void funcFloat(void)
{
	float myFloat;
	int ch;

	printf("\nEnter a decimal number: ");
	scanf("%f",&myFloat);

	/* Flushes input buffer */	
	while ((ch = getchar()) != '\n' && ch != EOF) ;

	printf("\nPress ENTER to return to MAIN MENU.");
	while ((ch = getchar()) != '\n' && ch != EOF);

	return;
}

void funcInteger(void)
{
	int myInt;
	int ch;

	printf("\nEnter a number: ");
	scanf("%i",&myInt);

	/* Flushes input buffer */	
	while ((ch = getchar()) != '\n' && ch != EOF) ;

	printf("\nPress ENTER to return to MAIN MENU.");
	while ((ch = getchar()) != '\n' && ch != EOF);

	return;
}

Recommended Answers

All 4 Replies

add it after " scanf("%f",&myFloat);" and "scanf("%i",&myInt);"

add it after " scanf("%f",&myFloat);" and "scanf("%i",&myInt);"

when i try it to add i and compile the code i got errors and warnings. And when i try to run the program it gives me an abort message with red x on it.

Are there any good suggestion to where i can add the code safely/properly without getting any errors and warnings.

when i try it to add i and compile the code i got errors and warnings. And when i try to run the program it gives me an abort message with red x on it.

Gee, since there are only about 2000 possible errors, all we can say is "you did it wrong." Would you care to point the problem?

Are there any good suggestion to where i can add the code safely/properly without getting any errors and warnings.

Considering where you did add it, either
1) No there
2) With the correct syntax
3) Both

Please keep in mind we are not psychic and not able to read your mind. Neither can we see what's on your screen. Therefore, to get help it is YOUR job to give us the details we need to understand your problem.

Gee, since there are only about 2000 possible errors, all we can say is "you did it wrong." Would you care to point the problem?


Considering where you did add it, either
1) No there
2) With the correct syntax
3) Both

Please keep in mind we are not psychic and not able to read your mind. Neither can we see what's on your screen. Therefore, to get help it is YOUR job to give us the details we need to understand your problem.

Nevermind i figured how to add the code. Thanks for the help anyways

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.