I have a project for my class that basically reads 3 students info (each student has 3 courses, and 5 tests, along with info like their name, ssn, phone number, etc.) from an input file, stores the input in arrays, then outputs the information into an output file. The project must be split into 6 functions, not including the main function.
I can successfully read and store the information, but when i go to the "validateData" function, my array is not being passed through to the function, so it will not be able to validate it. This error is also happening throughout the other functions as well. I believe it is probably an error with the prototypes of the functions, as that is what i am mostly confused about it.
i have tried changing the prototype parameters to (string[],int[],double[]) because i have 3 different arrays, one for the strings, one for the ints, and one for the doubles. But this has not worked. I also tried the same thing except without these "[]", still was not working, i believe the error said that the compiler was unable to change a double* into a double, or vice versa.
//Declaring global constants
const int STUDENTS = 3;
const int NONNUMERIC_INFO = 9;
const int NUMERIC_INFO = 2;
const int COURSES = 3;
const int TESTS_and_CNG = 6;
//Above main function, Function prototype
void validateData(string [], int [], double []);
//Inside main function, Function call
validateData();
//Declaring Arrays (inside of a different function called inputData)
string nonNumeric1[STUDENTS][NONNUMERIC_INFO];
int numeric1[STUDENTS][NUMERIC_INFO];
double numeric2[STUDENTS][COURSES][TESTS_and_CNG];
//Under main function, Function definition
void validateData(string, int, double)
{
//A bunch of if statements that i will soon turn into for loops, but will save you guys the trouble of reading them all, as its not important to the question.
}
I am expecting the arrays to be passed through into the function, however it is not doing so and giving me an error saying "error: too few arguments to function 'void validateData(std::__cxx11::string*, int*, double*)'" I am unaware of what the error message is trying to say.
Good job with the prototype function but your function title lines don't match.
Your Prototype:
//Above main function, Function prototype
void validateData(string [], int [], double []);
Your Function:
void validateData(string, int, double)
Secondly, although your prototype may no need variable names, your function title line certainly does.
void validateData(string, int, double) your function here only has variable types. I'd check out a tutorial like declaring functions if you aren't fully familiar with declaring functions.
Finally, once you've matched up your prototype function title line and your actual function title line and gave your actual function parameters variable names, you'll need to fix your function call.
You call your function like so: validateData(); This again, doesn't match your function title line. Your function title line says that validateData takes a string array, int and a double. Your function call passes nothing (). If you want your array, int and double to appear in the function you need to pass them in the function call. validateData(some_array, some_int, some_double).
Don't forget to replace some_array, some_int ... etc with actual variables with their respective types.
Edit:
If you're looking to call the validateData method inside inputData you'd have to do something like:
void inputeData(....) {
validateData(some_array, some_int, some_double);
}
Related
I'm trying to understand a Function/Method in a Library in order to port it to Java however some parameters don't make any sense to me and reading the source code the library is based on is not helping.
Function (Note the API has few comments (We can also ignore the calc handle since it's got a supplier method))
Ssr calc_ssr(CalcHandle *calc, NoteInfo *rows, size_t num_rows, float music_rate, float score_goal) {
std::vector<NoteInfo> note_info(rows, rows + num_rows);
auto skillsets = MinaSDCalc(
note_info,
music_rate,
score_goal,
reinterpret_cast<Calc*>(calc)
);
return skillset_vector_to_ssr(skillsets);
}
NoteInfo Struct
struct NoteInfo
{
unsigned int notes;
float rowTime;
};
MinaSDCalc
// Function to generate SSR rating
auto
MinaSDCalc(const std::vector<NoteInfo>& NoteInfo,
const float musicrate,
const float goal,
Calc* calc) -> std::vector<float>
{
if (NoteInfo.size() <= 1) {
return dimples_the_all_zero_output;
}
calc->ssr = true;
calc->debugmode = false;
return calc->CalcMain(NoteInfo, musicrate, min(goal, ssr_goal_cap));
}
Calc expected input file data (Only care about the #Notes: ...)
Pastebin
Question
What is NoteInfo in calc_ssr, I don't know any C or C++ so the *rows to me just seems like a pointer to a Noteinfo instance, however the MinaSDCalc methods requires an Array/Vector which using a pointer to a single instance doesn't make sense to me (pairing this with the fact that NoteInfo needs another parameter rowTime which I think is time of Note occurrence in the file which means that value must not be constant otherwise the produced result would be inaccurate)
Github Project: https://github.com/kangalioo/minacalc-standalone (The code alone may not explain enough but it's worth a try; best to look at API.h and discern what's used from there. Though I do warn you a lot of the Code is esoteric)
Sorry if this doesn't make much sense but I've been looking into this since June/July and this API is the closest abstraction from the bare C++ code I could find.
NoteInfo * rows here is pass by pointer. So, rows actually is a pointer to an instance of type NoteInfo. This is one of the ways to pass arrays in c++ to a function. Since arrays are contiguous in memory so we can just increment the pointer by one and get the next element of the array.
for example look at these three ways to do exactly one thing, parameter to pass an array to a function :-
1. void myFunction(int *param) {}
2. void myFunction(int param[10]) {}
3. void myFunction(int param[]) {}
Look into this link for more understanding : https://www.tutorialspoint.com/cplusplus/cpp_passing_arrays_to_functions.htm
Also search for pass by pointer and pass by reference to look into different ways of passing arguments in c++.
2.however the MinaSDCalc methods requires an Array/Vector which using a pointer to a single instance doesn't make sense to me: as to this question of yours, you can now see MinaSDCalc is actually getting an array and not a single instance as passing the pointer is also one of the ways of passing an array in c++.
I have 10 functions with generic names which return references like:
int& function0();
int& function1();
...int& functionN();
As you can see only one letter changes in the function name. Each function does a different job. My use case is given some length, say L, I have to call functions from 0 to L. So I want to somehow generate these function names and call them instead of hardcoding all the function calls based on L. So if in a loop where index i is from 0 to L, for each i I want to call functioni().
One approach that I tried was to store these functions into an array of function pointers but that does not work as these functions return references and array of references is not possible. I also tried to use macro concatenation to generate function names but that is also not possible because macro cannot substitute the value of some variable at preprocessing (MACRO(function,i) does not substitute value of i, concatenates to functioni).
How can I do such thing in C++?
That's not possible in C++, but in your case an array of function pointers seems like a good solution:
typedef int& (* functionPtr)();
functionPtr functions[N];
// or without typedef: int& (* functions[N])();
functions[0] = foo; // assign a function named foo to index 0
int& i = functions[0](); // call function at index 0
You could store an array of function pointers, as answered by Zenith.
In C++11, you could have an array of function closures, e.g. use std::function and lambda expressions. You could have some std::map<std:string,std::function<int&(void)>> associating names to closures.
You could wrap the calls in some function like
int& dofunction(int n) {
switch 0: return function0();
switch 1: return function1();
/// etc...
};
and you could write some small script (e.g. in awk, python, shell, etc....) to generate the C++ code of the above dofunction.
At last, on some operating systems (e.g. Linux and most POSIX), you could retrieve a function pointer at runtime by its name using dlopen(3) (with a NULL filename) then dlsym. You would then declare extern "C" int& function0(); (to avoid name mangling -otherwise you need to pass the mangled name to dlsym) and you need to link the program with -rdynamic -ldl; see C++ dlopen mini howto.
void PrintPosition (int *,int *,char* arrayLine);
This is my prototype function before int main().
char arrayLine[71]="----------------------------------------------------------------------";
PrintPosition (&Tortoise,&Hare,&arrayLine[]);
This is my int main() where I want pass down arrayLine[71] to a function called PrintPosition.
void PrintPosition (int *Tortoise, int *Hare, char *arrayLine)
{
char arrayLine[71]="----------------------------------------------------------------------";
if(*Tortoise==*Hare)
{
arrayLine[*Tortoise+0]='O';
arrayLine[*Tortoise+1]='U';
arrayLine[*Tortoise+2]='C';
arrayLine[*Tortoise+3]='H';
}
else
{
arrayLine[*Tortoise]='T';
arrayLine[*Hare]='H';
}
cout<<arrayLine<<endl;
}
This obviously doesn't compile at all.
What I am intending to do here, is to pass down the whole character array with size 71 to function, then manipulate char array at the function.
The reason why I am trying this is because I get stack frame error, buffer overrun error, stack around the variable 'arrayLine' was corrupted error. <---when I just declared arrayLine[71] in the function without passing through it.
So the question is, what is the possible method for me to pass down the whole array as I intended?
The way to pass the array to the function is:
PrintPosition (&Tortoise,&Hare,arrayLine);
This is a pass by pointer (with slightly confusing syntax). It is not possible to pass a C-style array by value.
To pass an array by value you would have to use a different sort of array, such as std::array. Using C-style arrays in C++ is discouraged because they do not behave like values.
Of course, you should remove the definition of arrayLine within PrintPosition .
I have a very simple question that I'm sure can be answered quite easily, but google is not exactly yielding results that are helping rectify the error I am receiving.
I have a simple single-dimension array consisting of 100 integers.
I am required to use another function to "fill" the array, so to speak.
While in main I am trying to make a simple call to the function that will "fill" the array with random integers.
However, I have written the call-to as such:
fillArray (int list[], int number);
With number being the # of random integers to store in the array. These parameters cannot change.
I keep receiving the error message:
"Expected '(' for function-style cast or type construction.
I have only been in my C++ class for 5 weeks now and I really have no idea what that means. I'm self taught for the most part in CSS, VBA, MySQL, and HTML of course. So I'm not as...intuitive when it comes to translating error messages.
Spent a while trying to google what exactly it meant and all the codes were way beyond my scope of understanding haha.
Here is my declaration of the function that I have within my main function (this is a requirement).
void fillArray(int, int);
And my function header:
void fillArray (int array[], int n)
I do not have anything written in the fillArray function as of yet.
Could that be the reason for the error?
If it is I will likely feel a bit foolish. :")
I think you just need to declare an array of integers as the first parameter:
void fillArray(int[], int);
You code should have the following structure:
void fillArray(int[], int);
int main()
{
//...write code to create array here ...
//this is how you call the function
fillArray (list, number);
}
void fillArray (int list[], int number)
{
//...write your implementation of fillArray here
}
I am working on a piece of structured programming homework that requires that I make a program that allows the user to enter names blah blah blah and so on. What I want to do after putting names into the string array is to print them to the screen. I had hoped to accomplish this by passing the array and the number of names contained therein to a function that would then print them to the screen. I wanted to pass the array and number of names as constants so that it would safeguard them so they couldn't be modified by the function, just read-only. I don't understand why I can't put const before the string array or the number of names though.
void writeNames (const string namelist[], const int number_of_names)
Is this something I just have to accept or is there a way I can pass both of those as read-only to the function? I can complete the homework without this so this is more a question of curiousity than a "help me with my homework" one.
P.S. Vectors seem to be a way of doing a lot more things with strings and such, but we haven't got to them in class yet and therefore I can't use them yet either.
Thanks
Without seeing what you're doing within the function, it's hard to answer the question, but my crystal ball says that you're probably doing something that could potentially modify one of the parameters within your method, so the compiler is complaining because you declared the parameter const.
As an example, something like this works just fine:
void writeNames(const std::string namelist[], const int number_of_names)
{
for(int i = 0; i < number_of_names; i++) {
std::cout << namelist[i] << std::endl;
}
}
However, something like this would cause a compiler error, since you're changing one of the variables:
void writeNames(const std::string namelist[], const int number_of_names)
{
while(number_of_names--) { // <-- We're modifying a const value here
std::cout << namelist[count] << std::endl;
}
}
Incidentally, putting the const modifier on your "number_of_names" parameter is somewhat redundant since you're passing the parameter by value, so changing the value within the function would have no effect on the value of the input parameter in the calling function. A change in any of the strings in the array, however, would be reflected in the calling function, so the const modifier there makes sense. Generally you would use const only on parameters that are either pointers, or being passed in by reference.