473,320 Members | 1,746 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,320 software developers and data experts.

problem with a function!

1
I've been developing a programm similar to the stable marriage problem.
I've just started but there is something wrong with the code. The 3rd argument of function saveToFile doesn't work. Can somebody help me?


Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <io.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. //using namespace std;
  6. void saveToFile(char* fileName, int numberOfPersons, char *namesOfPersons)
  7. {
  8.     FILE *myFile;
  9.     if((myFile=fopen(fileName,"w"))==NULL)
  10.     {
  11.         printf("Cannot write file... Using already stored lists.");
  12.         exit(-1);
  13.     }
  14.     else
  15.     {
  16.         for(int i=0;i<numberOfPersons;i++)
  17.         {
  18.                 fprintf(myFile, "%s\n",&namesOfPersons[i]);
  19.         }
  20.         fclose(myFile);
  21.     }
  22. }
  23.  
  24. void dataEntry(char* requestFor)
  25. {
  26.     int numberOfPersons;
  27.  
  28.     printf("How many %ss? ",requestFor);
  29.     scanf("%d",&numberOfPersons);
  30.  
  31.     char *namesOfPersons[numberOfPersons]; //Array of character array(string);
  32.  
  33.     //Ask for Person names    
  34.     for(int i=0;i<numberOfPersons;i++)
  35.     {
  36.       printf("%s name %d: ",requestFor,i);
  37.       scanf("%s",&namesOfPersons[i]);      
  38.     }
  39.        saveToFile(strcat(requestFor,"List.txt"), numberOfPersons, &namesOfPersons);
  40. }
  41. int main(int argc, char *argv[])
  42. {
  43.     dataEntry("Teacher");
  44.     dataEntry("Student");
  45.     dataEntry("Diplwmatikes");
  46.     system("PAUSE");
  47.     return EXIT_SUCCESS;
  48. }
  49.  
I can't put it in code tags for some reason!
Mar 4 '10 #1
1 1268
donbock
2,426 Expert 2GB
Whenever you pass an array you need to declare the parameter as a pointer to the first element in the array. The first element is a null-terminated string; that is, a char*. Your parameter has to be a pointer to a char* ... or a char**.

Function dataentry defines automatic variable namesOfPersons to hold the array of pointers, but it doesn't allocate any memory to hold the strings pointed to by the array elements. You should review the operation of the "%s" feature of scanf.
Mar 4 '10 #2

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

Similar topics

1
by: Covad | last post by:
Hi all, For some reason my change() function is only called when the page loads. I'd much rather it gets called when the select changes. Here's the code: window.onload = init; function...
7
by: Emanuel Ziegler | last post by:
Hello, I want to do some mathematics with functions. In my case the function classes are very complex, but this simple example has the same problems. To allow calculations that begin with a...
117
by: Peter Olcott | last post by:
www.halting-problem.com
1
by: Mohamed Fysal | last post by:
I have written a Regular DLL with many Export Functions and one CALLBACK fun ction . The callback function declared in the .cpp file of the Regular DLL is as fol lows: typedef BOOL...
4
by: Andy_Khosravi | last post by:
Hello, I'm having a problem with the MID function within Access 97. I have been trying to build a function to check to make sure that a field on a form does not have any spaces or dashes. This...
0
by: Lucas, Todd | last post by:
Hello everyone! I'm having a problem with a WebControl that I'm designing for a Menu. I've been at it for about 3 weeks now, and can't seem to get around this problem. So I'm hoping that someone...
78
by: Josiah Manson | last post by:
I found that I was repeating the same couple of lines over and over in a function and decided to split those lines into a nested function after copying one too many minor changes all over. The only...
5
by: jbenner | last post by:
I have opened a PMR for this with IBM, and am not asking for advice from the DB2 DBA community. I am posting this as an FYI that DB2 Health Monitor, even at the latest version of DB2, still can cause...
2
by: Ravikiranreddy | last post by:
Hi every one, Am pasting the error code returned in log please help me in fixing it--- ERROR:An error occurred during the execution of the command "/opt/IBM/db2/V8.1/instance/dascrt -u...
6
by: pauldepstein | last post by:
Let double NR( double x, double(*)(const double&) f ) be the signature of a Newton-Raphson function NR. Here, f is a function which returns a double and accepts a const double&. The aim of...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.