Welcome to the GoFuckYourself.com - Adult Webmaster Forum forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Post New Thread Reply

Register GFY Rules Calendar
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >
Discuss what's fucking going on, and which programs are best and worst. One-time "program" announcements from "established" webmasters are allowed.

 
Thread Tools
Old 02-22-2008, 07:56 PM   #1
Kard63
Confirmed User
 
Kard63's Avatar
 
Join Date: Nov 2003
Location: 237 619 975
Posts: 8,944
Anyone know C ? noob stuff

Why does my menu print twice everytime it loops?

**************************
#include <stdio.h>

void add() {
int first;
int second;
printf("First integer: ");
scanf("%d", &first);
printf("Second integer: ");
scanf("%d", &second);
printf("Answer: %d\n\n", first + second);
}

void sub() {
int first;
int second;
printf("First integer: ");
scanf("%d", &first);
printf("Second integer: ");
scanf("%d", &second);
printf("Answer: %d\n\n", first - second);
}

void mul() {
int first;
int second;
printf("First integer: ");
scanf("%d", &first);
printf("Second integer: ");
scanf("%d", &second);
printf("Answer: %d\n\n", first * second);
}

void div() {
int first;
int second;
printf("First integer: ");
scanf("%d", &first);
printf("Second integer: ");
scanf("%d", &second);
if (second == 0)
printf("The second integer must be nonzero.\n\n");
else
printf("Answer: %d\n\n", first / second);
}

void pow1() {
int first;
int second;
printf("First integer: ");
scanf("%d", &first);
printf("Second positive integer: ");
scanf("%d", &second);
if (second <= 0)
printf("The second integer must be positive.\n\n");
else {
int answer = first;
int i;
for (i = 1; i < second; i++)
answer *= first;
printf("Answer: %d\n\n", answer);
}
}

void gcd() {
int first;
int second;
printf("First integer: ");
scanf("%d", &first);

printf("Second positive integer: ");
scanf("%d", &second);
if (first <= 0 || second <= 0)
printf("Integers must be positive.\n\n");
else {
if (first < second) {
int temp = first;
first = second;
second = temp;
}
while (second != 0) {
int remainder = first%second;
first = second;
second = remainder;
}
printf("Answer: %d\n\n", first);
}
}

void fac() {
int first;
int second;
printf("First integer: ");
scanf("%d", &first);
if (first <= 0)
printf("The integer must be positive.\n\n");
else {
int i;
for(i = first-1; i > 0; i--) {
first *= i;
}
printf("Answer: %d\n\n", first);
}
}


int main(void)
{
char choice;
int run = 1;
while (run) {
printf("----------------------------------------------\n");
printf("| 1. Add two integers. |\n");
printf("| 2. Substract two integers. |\n");
printf("| 3. Multiply two integers. |\n");
printf("| 4. Divide two integers. |\n");
printf("| 5. Integer raised to an integer power. |\n");
printf("| 6. Compute GCD of two integers. |\n");
printf("| 7. Compute factorial of an integer. |\n");
printf("----------------------------------------------\n");
printf("What would you like to do? ");
choice = getchar();
printf("\n");
switch(choice)
{
case '1':
add();
break;
case '2':
sub();
break;
case '3':
mul();
break;
case '4':
div();
break;
case '5':
pow1();
break;
case '6':
gcd();
break;
case '7':
fac();
break;
case 'q':
run = 0;
break;
case 'Q':
run = 0;
break;
}
}
return 0;
}
__________________
Kard63 is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 02-22-2008, 10:27 PM   #2
Kard63
Confirmed User
 
Kard63's Avatar
 
Join Date: Nov 2003
Location: 237 619 975
Posts: 8,944
enter was putting /n in the buffer bla bla I solved it.
__________________
Kard63 is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 02-23-2008, 02:39 AM   #3
Mr. Mojo Risin
Confirmed User
 
Join Date: Jul 2003
Location: In the middle of nowhere
Posts: 1,886
why didn't you just use cin and cout for input and output?

also, I don't know what IDE you are using but you should manually format things by using tab or the format selection feature in Visual Studio

It's easier to read that way

Last edited by Mr. Mojo Risin; 02-23-2008 at 02:41 AM..
Mr. Mojo Risin is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Old 02-23-2008, 04:07 AM   #4
mortenb
Confirmed User
 
mortenb's Avatar
 
Join Date: Jul 2004
Location: Denmark ICQ: 7880009
Posts: 2,203
also you might want to consider a switch statement or something like that instead of all these functions
mortenb is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote
Post New Thread Reply
Go Back   GoFuckYourself.com - Adult Webmaster Forum > >

Bookmarks



Advertising inquiries - marketing at gfy dot com

Contact Admin - Advertise - GFY Rules - Top

©2000-, AI Media Network Inc



Powered by vBulletin
Copyright © 2000- Jelsoft Enterprises Limited.