I have a homework assignment that I have been trying to figure out for over 12 hours now...The program I am trying to write is too complicated to post, so I created a simpler program which lets a user enter two integers and an operation to perform. If someone can help me figure out how to write the pointers correctly so that I get an output, I can probably figure out my homework assignment (I made an attempt with the addFunc). The other problem I am having is that when I take out the pointers to check my algorithms, if you enter in a value other than 'a', 's', 'm', or 'd', it gets stuck in the while loop. Please help. :sad:

#include "input.h" 
#include <stdio.h>
#include <ctype.h>

typedef int bool;
#define true 1
#define false 0

void addFunc(int *x, int *y);
void subFunc(int x, int y);
void multFunc(int x, int y);
void divFunc(int x, int y);

int a, b, total;
char operation, throwaway;
bool badOperation;

int main()
{
	printf ("Please enter an integer: ");
	scanf ("%d", &a);
	printf ("Please enter another integer: ");
	scanf ("%d", &b);

	printf ("Would you like to (a)dd, (s)ubtract, (m)ultiply, or (d)ivide?\n");
	scanf ("%c", &operation);
	scanf ("%c", &throwaway);

	printf ("%c", &operation);

	if (operation != 'a' && operation != 's' && operation != 'm' && operation != 'd')
	{
		badOperation = true;
	}

	while (badOperation)
	{
		printf("You must select from 'a', 's', 'm', or 'd'.\n");
		scanf("%c", &operation);
		scanf("%c", &throwaway);

		if (operation == 'a' || operation == 's' || operation == 'm' || operation == 'd')
		{
			badOperation = false;
		}
	}

	if (operation == 'a')
	{
		addFunc(a, b);
		printf ("The sum is %d.\n", total);
	}

	if (operation == 's')
	{
		subFunc(a, b);
		printf ("The difference is %d.\n", total);
	}

	if (operation == 'm')
	{
		multFunc(a, b);
		printf ("The product is %d.\n", total);
	}

	if (operation == 'd')
	{
		divFunc(a, b);
		printf ("The quotient is %d.\n", total);
	}
}

void addFunc(int *x, int *y)
{
        int *x = &a;
        int *y = &b;
	int total;

	total = *x + *y;
}

void subFunc(int x, int y)
{
	int total;

	total = x - y;
}

void multFunc(int x, int y)
{
	int total;

	total = x * y;
}

void divFunc(int x, int y)
{
	int total;

	total = x / y;
}

Try printing out the characters that you read, and compare what you expected to get with what you actually got.

yeah, my mom suggested that I do that to see what is stored in 'operator', and when I do, weird characters show up, like an upside down question mark or a small "1/4"

Wow you butcher-ed that one nice now didn't ya. I haven't done C in a lil while but i'll show ya what ya did wrong.

First thing i did was kill that typedef to bool. I prefer to just use int straight forward even though bool's built into my Visual C++ (i think).

Next obvious thing is when you pass a variable into a method/function in C or Java, at least, you dont have to re-assign the variables.

// this is wrong...
void addFunc(int *x, int *y)
{
int *x = &a;
int *y = &b;
int total;
total = *x + *y;
}

You have total declared as a global and then redeclaring it as a local. This function should look like this...

void addFunc(int *x, int *y)
{
        // Since these are pointers i am using the ' * ' deref opperator
        // to get the value of the pointer.  But not sure why pointers 
        // need to be used n e way/
	total = *x + *y;
}

Next thing that i can remember. Why do you have 2 scanf methods after a single input? Are you going to do something with the "ThowAway" that i dont see? I suggest using this method of scanf that will remove extra leading white space to read a char.

scanf (" %c",&operation);

That should give you a huge start. :)

When I do what you say, I get this message:

"Please enter an integer: 1
Please enter another integer: 1
Would you like to (a)dd, (s)ubtract, (m)ultiply, or (d)ivide?
a
Bus error (core dumped)"

i found your mistake. you mixed up "call by reference" and "call by value" which is not allowed in C. in this case while you are calling the addfun and the other functions you are passing the variables "a" and "b" which you are trying to store in a pointer.
remember a pointer can only store the address of a variable.
so replace the actual arguments with addfun(&a,&b) instead of a and b.
this ought to definitely work.

This article has been dead for over six months. Start a new discussion instead.