r/CircleProgramming • u/Illuminatesfolly • Feb 11 '13
Guise, I need serious help with a complex mess of pointers. Please fix this code.
/*****************************************************************************
* Chris Campbell (cacampbe) and Denney Kwok (denneyk)
* ECS 30 Homework 3 Problem 6
* pointers.c
*****************************************************************************/
/**
* Reminder.
* Main() should be the first function.
* Prototypes must define functions before main()
* No Loops, Arrays or Global Variables (Fields)
*/
//Inclusions and Definitions
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
//Fields
//Prototypes
void operate(int, int, int);
void operate2(int, int, int, int);
void InitialPrint();
void SecondPrint();
bool CheckAddress(int, int, int, int);
void Operations(int, int);
bool Restart1(int, int, int, bool);
bool Restart2(int, int, int, bool, int);
//Methods
int main()
{
int a, b, c;
setvbuf(stdout, NULL, _IONBF, 0);
printf("The addresses are num: %u, num2: %u, num3: %u\n",
(unsigned) &a, (unsigned) &b, (unsigned)&c);
operate(&a, &b, &c)
printf("Done.\n");
return(0);
} //main()
void operate(int* a, int* b, int* c)
{
int* firstaddress;
bool test;
bool restart;
InitialPrint();
scanf("%u", (unsigned*) &firstaddress);
test = CheckAddress(a, b, c, firstaddress);
restart = Restart1(a, b, c, test);
if (restart)
{
operate2(a, b, c, firstaddress);
}
return;
} //operate()
void operate2(int* a, int* b, int* c, int* firstaddress)
{
int* secondaddress;
bool test;
bool restart;
SecondPrint();
scanf("%u", (unsigned*) &secondaddress);
test = CheckAddress(a, b, c, secondaddress);
restart = Restart2(a, b, c, test, firstaddress);
if (restart)
{
Operations(firstaddress ,secondaddress);
}
return;
} //operate2()
void InitialPrint()
{
printf("Please enter int address #1: ");
return;
} //InitialPrint()
void SecondPrint()
{
printf("\nPlease enter int address #2: ");
return;
} //InitialPrint()
bool CheckAddress(int a, int b, int c, int address)
{
bool test = false;
if (address == (unsigned)&a || address ==
(unsigned)&b || address == (unsigned)&c)
{
printf("That is a valid address.\n");
test = true;
}
else
{
printf("That is not a valid address.\n");
}
return test;
} //CheckAddress1()
bool Restart1(int a, int b, int c, bool test)
{
bool otherBool = false;
if (test)
{
otherBool = true;
}
else
{
operate(a, b, c);
}
return otherBool;
} //Restart()
bool Restart2(int a, int b, int c, bool test, int firstaddress)
{
bool otherBool = false;
if (test)
{
otherBool = true;
}
else
{
operate2(a, b, c, firstaddress);
}
return otherBool;
} //Restart2()
void Operations(int firstaddress, int secondInt)
{
int firstInt;
int operand;
char operator;
printf("Please enter an integer: ");
scanf("%d", (unsigned*) &operand);
printf("\nPlease enter an operator: ");
scanf("%c", &operator);
switch (operator)
{
case '+':
firstInt = firstaddress;
secondInt = firstInt + operand;
printf("\nFirst int: %d, second int: %d.\n", firstInt, secondInt);
break;
case '*':
firstInt = firstaddress;
secondInt = firstInt * operand;
printf("\nFirst int: %d, second int: %d.\n", firstInt, secondInt);
break;
case '=':
firstInt = firstaddress;
secondInt = firstInt;
printf("\nFirst int: %d, second int: %d.\n", firstInt, secondInt);
break;
case '%':
firstInt = firstaddress;
secondInt = firstInt % operand;
printf("\nFirst int: %d, second int: %d.\n", firstInt, secondInt);
break;
default:
printf("\n%c is an invalid operator.\n", operator);
break;
}
return;
}//Operations()
7
Upvotes
5
u/[deleted] Feb 11 '13
What're the errors? What do you expect it to do? What is it doing?
I'm too lazy to gcc it and see for myself
5
u/cokeisahelluvadrug Feb 11 '13 edited Feb 11 '13
Go ahead and paste the errors in this thread, the errors usually tell you what the problem is. Consider developing iteratively and you'll run into less errors like this
edit: also comment your code, especially if you want people to be able to read over it and understand what it is doing. Naming your variables and functions is not enough
3
u/Gravemind123 Feb 11 '13
Ok, so your first problem is that you are using int* in places where you should be using int in a shitload of places. Like, I have two terminal windows full of GCC warnings along those lines. Go through and make sure you aren't doing that. If you have an int* and need an int, dereference the pointer. If you have an int and need an int* pass it by reference.
Also, in your definition at the top you define operate as taking (int a, int b, int c) but later on you define it as taking (int* a, int* b, int* c)
8
u/lolsail Feb 11 '13
Can I get a tl;dr?
7
u/Carl_DePaul_Dawkins Feb 11 '13
4
u/CoyoteStark Feb 11 '13
You forgot a parenthesis.