# Maximum PC

 It is currently Tue Mar 11, 2014 8:11 pm

 All times are UTC - 8 hours

 Page 1 of 1 [ 2 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Need help with C programPosted: Thu Sep 19, 2013 10:24 am
 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

 Post subject: Re: Need help with C programPosted: Thu Sep 19, 2013 12:31 pm
 Clawhammer

Joined: Sun Jun 18, 2006 7:37 pm
Posts: 4350
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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 2 posts ]

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 2 guests

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

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group