473,412 Members | 4,594 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,412 software developers and data experts.

Pointers Adresses and getting data byRef from a functions

OK... have been working on a class project and I am about at my wits ends...

First using intel / XP-Pro / TurboC++4.5 (don't ask me why) / Using a dell D610

I get the concept. I get the usage. I just can not get it to work. I believe the problem is a memory error but can not figure out why.

I kept the code as short as possible (I won't post my entire program unless I need to...

Here's the skinny: this is a gas and go assignment where you input day of week as 0-6, gallons, type of gas (R,U,S), and if you want your car washed. Prices are defined as #define ...

** Not all of code is posted to save space. Besides I want to work through the problem one function at a time...

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <stdio.h>
  3. #include <conio.h>
  4.  
  5. //input data function
  6. void getData (int *dNum, int *gal, char *gType, char *wAns);
  7.  
  8. int main(void)
  9. {
  10.  
  11. // DECLARE VARIABLES
  12.    int dayNum, gallons;
  13.    char response, gasType, washAns;
  14.    float unitPrice, washCost, gasCost, total;
  15.  
  16. getData(&dayNum, &gallons, &gasType, &washAns);  // func call getData
  17.  
  18.    return 0;   // program ran sucessful
  19.  
  20. }
  21.  
  22. // FUNCTION CALLS
  23.  
  24. void getData(int *dNum, int *gal, char *gType, char *wAns)
  25. {
  26.    clrscr();                // clear screen
  27.  
  28. // get day of week
  29.    printf("\nEnter day of the week (0-6)");
  30.    printf("\nSun=0, Mon=1, Tue=2, ..., Sat=6 : ");
  31.    scanf("%d", dNum);
  32.  
  33. // get Number Of Gallons Pumped
  34.    printf("\n\nEnter the number of gallons : ");
  35.    scanf("%f", gal);
  36.  
  37. // get Type of Gas Purchased
  38.    printf("\n\nEnter gas type");
  39.    printf("\n\(R, U, S or N for no gas\) : ");
  40.    fflush(stdin);
  41.    scanf("%c", gType);
  42.  
  43. // get Car Washed?
  44.    printf("\n\nCar Wash \(Y or N\) : ");
  45.    fflush(stdin);     //clear buffer for scanf
  46.    scanf("%c", wAns);
  47.  
  48.  
  49.    return;                   // return to calling place
  50. }
  51.  
  52.  
The problem that I can see is that it input the correct data while in the function but once I leave the function all the variable change as either they are being over written in memory. We have not offically started to use malloc() at this point and all the practice assignments that I have been given, this format matches exactly. Any help would be a blessing...
Mar 16 '10 #1

✓ answered by weaknessforcats

Expand|Select|Wrap|Line Numbers
  1.  scanf("%f", gal); 
gal is an int*. %f says it's a float. Hmm...

I didn't look too closely at other stuff but I owuld suggest ou initialize all variables before using them in function calls.

2 1455
weaknessforcats
9,208 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1.  scanf("%f", gal); 
gal is an int*. %f says it's a float. Hmm...

I didn't look too closely at other stuff but I owuld suggest ou initialize all variables before using them in function calls.
Mar 16 '10 #2
I picked up on that about midnight last night. I have most of it working now. I still have 1 more function to work through to get the whole program working. That is the big one where I bring everything together. Will up date when I get stuck in that section.. Thanks Weak for the point out...
Mar 16 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Jan-Henrik Grobe | last post by:
Hallo, I am coming to this newsgroup with a very strange Problem. I have two c++ files A.cpp and B.cpp....In A.cpp I am creating an openGL window and in B.cpp I have stored the callback...
8
by: Salad | last post by:
I designed a small app and I wanted to do a BrowseFolder (see http://www.mvps.org/access/api/api0002.htm), basically do a file open diaglog and select a directory/folder. The problem is that you...
9
by: Mikhail Teterin | last post by:
Hello! I'd like to have a variable of a pointer-to-function type. The two possible values are of type (*)(FILE *) and (*)(void *). For example: getter = straight ? fgetc : gzgetc; nextchar...
8
by: Bryan G | last post by:
Hi, I'm working on a VB project which involves using C library functions which take struct pointers as args, and I keep running into this error when trying to pass either an IntPtr or a Structure...
4
by: ItsMe | last post by:
Hi Guyz, I'm unable to understand this (AddressOf) error??? In VB6 I have two functions: ---------------------------- Public Function ImageFirstImageCallback(ByVal hWnd As Integer, ByVal...
15
by: John Alway | last post by:
Hello, I'm using a DLL I wrote in C++, and am attempting to call and use it from VB. This works fine for functions where I pass parameters by value, but I can't get pointers to work. I get...
1
by: plau011 | last post by:
Hi all, I thought I understood .Net Framework with value types and referencetypes but apparently not. So I have this code. Excuse me if it's not 100% correct I cut and paste and shortened it a...
1
by: Lyle Fairfield | last post by:
Option Explicit ' requires VBScript to be installed ' (maybe don't give this to your sugnificant other as ' it gets deleted addresses as well as current) ' obvious fixups needed '1. how get...
2
by: rocketfire97 | last post by:
I'm trying to call a COM object using C# but having no luck getting values back for passed in ref objects. I've tried the same call using VB.NET and can get data back. How would I implement the...
9
by: Matt | last post by:
Hi friends, Okay so well, I have quite a problem right now with a file stream. What I am doing is to use the Cannon SDK dlls to get control over my old Cannon A60 Camera for some surveillance...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.