Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 02-08-2005, 03:42 AM   #1 (permalink)
Upright
 
[C] problems with storing varilbes

I have a simple project for my intro to C class inwich we are suppose to pratice passing varibles through functions. It is a bit over modulized but that is the point of the exercies. The problem I am having is that when the user inputs two integers they are not being stored in the varibles. We have not gotten to pointers yet so I dont need to worry about that. I dont know what else to say so here is the code:

Code:
#include <stdio.h>
#include <math.h>

void getInput(int input1, int input2);
void calc(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);
void intOps(int input1, int input2, int sum, int iDiv, int iMod);
void floatOps(int input1, int input2, float half1, float half2, float frac);
double raiseToPower(int input1, int input2);
void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power);

int main(void) 
{
    int input1;
    int input2;
    int sum, iDiv, iMod;
    float half1, half2, frac;
    double power;

   
   getInput(input1, input2);
   calc(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
   display(input1, input2, sum, iDiv, iMod, half1, half2, frac, power);
   
   
   system("pause");
   return 0;
   
}//end of main

/*******************************************************************************
Name: getInput()
Purpose: This function prompts the usr for two integers and stores them in memory
*******************************************************************************/
void getInput(int input1, int input2) 
{
   
   
   printf("%20s", "Sean Todd");
   
   printf("\nPlease enter two integers: ");
   
   scanf("%d%d", &input1, &input2);
   
   printf("%20d%20d", input1, input2);
   
   
}//end of getInput()

/*******************************************************************************
Name: calc()
Purpose: This function takes the data the usr entered and calculates the sum,
 the half of each number entered
*******************************************************************************/

void calc(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power)
{
   printf("\n%20d %20d", input1, input2);

   intOps(input1, input2, sum, iDiv, iMod);
   floatOps(input1, input2, half1, half2, frac);
   power = raiseToPower(input1, input2);
   
}//end of calc()

/****************************************************************************
Name: intOps()
Purpose: perform all integer calculations
****************************************************************************/
void intOps(int input1, int input2, int sum, int iDiv, int iMod) 
{      

       sum = input1 + input2;
       iDiv = input1 / input2;
       iMod = input1 % input2;
       
}//end of intOps
   
/****************************************************************************
Name: floatOps()
Purpose: perform all integer calculations
****************************************************************************/
void floatOps(int input1, int input2, float half1, float half2, float frac) 
{
    half1 = (float) input1 / (2.0);
    half2 = (float) input2 / (2.0);
    frac = (float) input1 / (float) input2;
}//end of floatOps()
   
/****************************************************************************
Name: raiseToPower()
Purpose: perform all integer calculations
****************************************************************************/
double raiseToPower(input1, input2) 
{
   return pow(input1, input2);
   
}//end of raiseToPower()
   
/*******************************************************************************
Name: display()
Purpose: outputs the data to screen in report format
*******************************************************************************/
void display(int input1, int input2, int sum, int iDiv, int iMod, float half1, float half2, float frac, double power) 
{
    printf("\n%20s%20s", "Description", "Data");
    printf("\n%20s%20s", "-----------", "----");
    printf("\n%20s%20d", "Input1", input1);
    printf("\n%20s%20d", "Input2", input2);
    printf("\n%20s%20d", "Sum", sum);
    printf("\n%20s%20.1f", "Half of Input1", half1);
    printf("\n%20s%20.1f", "Half of Input2", half2);
    printf("\n%20s%20d", "Quotient", iDiv);
    printf("\n%20s%20d", "Remainder", iMod);
    printf("\n%20s%20.4f", "Fraction", frac);
    printf("\n%20s%20.0f", "Power", power);
    
    printf("\n\n");

}//end of display()
steve52 is offline  
Old 02-08-2005, 04:23 AM   #2 (permalink)
Insane
 
Location: Rio Grande Valley, Texas
The problem here is that you are making a a call to the getInput function by copy, not by reference.

If you replace this line:

void getInput(int input1, int input2)

with this:

void getInput(int& input1, int& input2)

and change the prototype as well, you might be in better shape. I'm not a a computer with a compiler right now, so I can't check it.
__________________
"I can't understand why people are frightened of new ideas. I'm frightened of the old ones." -- John Cage (1912 - 1992)
strcrssd is offline  
Old 02-08-2005, 09:48 AM   #3 (permalink)
Crazy
 
Location: Salt Town, UT
in order to do this without using pointers, you can't modify the variables that you are passing into the functions.

My recommendation would be to have getInput() return the integer that was input. Just call it twice, asking for one integer each time.

int getInput( void )
{
int my_input;

printf("Input a number: ");
scanf("%d",&my_input);
printf("\n");

return my_input;
}
Rawb is offline  
Old 02-28-2005, 08:18 AM   #4 (permalink)
Junkie
 
Rawb has the right idea if you are truely using C and not C++. Pass by refrence is a C++ thing, so to do the same in C you need to use pointers. Or just call the function twice and have it return 1 number each time.
Rekna is offline  
 

Tags
problems, storing, varilbes


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 09:10 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360