Quantcast

Maximum PC

It is currently Tue Oct 21, 2014 12:40 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Need help with C program
PostPosted: Thu Sep 19, 2013 10:24 am 
8086
8086

Joined: Thu Sep 19, 2013 10:21 am
Posts: 1
writing a simple program for my programming 2 class, last week we had to write the a program with the same function but we passed by value, and this one needs to be by reference. The teacher did not help much. Here is my code:


#include <stdio.h>

float addTen(float* num, float* total);
float subtractSixteen(float* num, float * total);
float multiplyByEleven(float* num, float* total);
float divideByTwelve(float* num, float* total);


int main ()
{
int selection = 1;
float num, total;

printf("Please enter a number: ");
scanf("%f", &num);
while (selection >0 && selection <5)
{
printf("\nWhich of the following functions would you like?\n\n\tEnter 1 to add 10\n\tEnter 2 to subtract 16\n\tEnter 3 to multiply by 11\n\tEnter 4 to divide by 12\n\tEnter 0 to Exit\n\nSelection: ");
scanf("%i", &selection);

float* ptr_num=NULL;
ptr_num = &num;
float* ptr_total=NULL;
ptr_total= &total;
switch(selection)
{
case 1:
total = addTen(&num);
num = total;
break;
case 2:
total = subtractSixteen(&num);
num = total;
break;
case 3:
total = multiplyByEleven(&num);
num = total;
break;
case 4:
total = divideByTwelve(&num);
num = total;
break;
default:
break;
}
printf("\nYour total is: %.2f\n", total);
}
}

float addTen(float* num, float* total)
{
*num = total + 10;
}

float subtractSixteen(float* num, float* total)
{
*num = total - 16;
}

float multiplyByEleven(float* num, float* total)
{
*num = total * 11;
}
float divideByTwelve(float* num, float* total)
{
*num = total / 12;
}


Top
  Profile  
 
 Post subject: Re: Need help with C program
PostPosted: Thu Sep 19, 2013 12:31 pm 
Smithfield
Smithfield

Joined: Sun Jun 18, 2006 7:37 pm
Posts: 5387
After looking at the code, I can point a few faults.
  • You don't need the ptr_num and ptr_total variables
  • Your calling your functions with one argument
  • There's a minor syntax error with the contents of those functions.
  • You're also not understanding the idea of pointers... maybe we should start with that first?


Top
  Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

© 2014 Future US, Inc. All rights reserved.