![]() |
a little pointer problem
Alright so I have a program that takes 2 player names and turns it into a file name. The file name looks like playerName1-playerName2.save. It then takes this file and searches the directory for it. If it finds it in the directory it is supposed to load the player names and the scores into local variables in main. It appears to load the names correctly but i can not figure out how to load the scores. I have a seperate function that goes through the file and picks out the names and scores from the file. Now i just need to figure out a way to get all of that into the local variables in main. I am pretty sure it can be done with pointers and pass by reference and by using &. Any help is appreciated.
Here's the code: :
#include <iostream>The input file has a name and then a space and then the score so it looks like this: :
sean 45I tested these function seperately so i know they will find the file and extract the information correctly now i just need to figure out how to assign that information to the local variables in main. And please note the line :
scanf("%s") |
Any particular reason you're mixing C and C++? It makes for a poor C program and a poor C++ program.
|
well i have to search the directory and the only way i know how to do that is in C and then the C++ part i had before that. So i guess it just kinda happened that way. Isn't the C just mainly the printf() that i used instead of cout? I didn't think it mattered that much if i mixed the 2.
|
Technically, most compilers don't care all that much if you mix C and C++, as long as you don't hit on incompatibilities between the languages. However, the languages have some subtle and not-so-subtle incompatibilities. So mixing C and C++ tends to give code that is much harder to get right (and you're having trouble getting your code working right), and also much harder for some poor sucker in future (who may even be yourself) to maintain. Code which is more likely to be incorrect, or more likely to break if you try to modify it, is viewed by most people as poor code.
There is more vanilla C in your code than using printf(). strcpy(), feof(), sprintf() are also in the standard C library, and most parts of the C library are deprecated (i.e. their usage is discouraged) in C++, as C++ provides alternate methods. Incidentally, scanf("%s") formally yields undefined behaviour according to the C standard (because the %s format descriptor causes the function to expect there to be a second argument, and that second argument is written to). To get values from a function back to main(), you need to do one of; 1) return the values to main() 2) move the values to some global area, which is accessed by both your function and by main() 3) pass pointer arguments to the function, so your function can modify whatever the pointer points at, and main() can see the corresponding variables change. |
alright, I'll clean up the C part and make it all C++. I just used the sprintf and feof cause i took a class in C las semester and I know thats a way to put together strings and find the end of a file. I dont know how to do that same thing in C++ although its probably easier, I just don't know how to do it so i went with what i knew and what i already had examples of how to do it. Now on to the other part. The third point you made:
Quote:
|
:
#include <iostream>Quote:
|
| All times are GMT -5. The time now is 2:43 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC