c++ - C++ Function Calling with Parameter in int main
问题描述
How would I go about calling the void menu function in the int main function? I tried different things for the parameter but i'm confused on how to call the menu function.
Updated It since required me to post the full Coding to better under stand my problem, but I just want a way to call the menu function.
//Compiling Error
WordData.cpp: In function 'int main()':
WordData.cpp:25:6: error: too few arguments to function 'void menu(std::string, int)'
25 | menu();
| ^
WordData.cpp:20:6: note: declared here
20 | void menu(string,int);
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include "WordData.h"
#include <fstream>
using namespace std;
//WordData.cpp
//prototype
void menu(string,int);
int main(){
//This problem here
menu();
//............
return 0;
}
void menu(string wrd,int cnt)
{
WordData wd = WordData(wrd,cnt);
int selection;
bool quit=false;
do
{
cout<<endl;
cout<< "1: PARALLEL ITERATIVE";
cin>>selection;
cout<<endl;
switch(selection)
{
case 1:
//this here
wd.setWord(wrd);
//.........
break;
case 2:
cout<<"Works 2";
break;
case 8:
quit=true;
}
} while(quit==false);
}
#ifndef WORDDATA_H
#define WORDDATA_H
#include <iostream>
#include <string>
using namespace std;
class WordData {
public: //PUBLIC FUNCTIONS
/*****************************************************************/
/* Function name: constructor */
/* */
/* Description: Will construct a default word data object.*/
/* Default values are an empty string zero as*/
/* the count. */
/* */
/* Parameters: string wrd - Word to be placed in object */
/* int cnt - Value to be placed in counter */
/* */
/* Member Type: */
/* */
/* Return Value: none */
/*****************************************************************/
WordData(string wrd = "", int cnt = 0);
//SETS
//
/*****************************************************************/
/* Function name: setWord */
/* */
/* Description: Will set the object's string. */
/* */
/* Parameters: string wrd - Word to be placed in object */
/* */
/* Member Type: */
/* */
/* Return Value: none */
/*****************************************************************/
void setWord(string wrd);
/*****************************************************************/
/* Function name: setCount */
/* */
/* Description: Will set the occurence counter. */
/* */
/* Parameters: int cnt - Value to be placed in counter */
/* */
/* Member Type: */
/* */
/* Return Value: none */
/*****************************************************************/
void setCount(int cnt);
/*****************************************************************/
/* Function name: setWordData */
/* */
/* Description: Will set both the object's string and the */
/* object's occurence count. */
/* */
/* Parameters: string wrd - Word to be placed in object */
/* int cnt - Value to be placed in counter */
/* */
/* Member Type: */
/* */
/* Return Value: none */
/*****************************************************************/
void setWordData(string,int);
//GETS
//
/*****************************************************************/
/* Function name: getWord */
/* */
/* Description: Will retrieve the object's string. */
/* */
/* Parameters: none */
/* */
/* Member Type: */
/* */
/* Return Value: string */
/*****************************************************************/
string getWord() const;
/*****************************************************************/
/* Function name: getCount */
/* */
/* Description: Will retrieve the occurence counter. */
/* */
/* Parameters: none */
/* */
/* Member Type: */
/* */
/* Return Value: int */
/*****************************************************************/
int getCount() const;
/*****************************************************************/
/* Function name: operator ++ */
/* */
/* Description: Will increment the occurrence counter. */
/* */
/* Parameters: int inc - Value to be incremented by */
/* */
/* Member Type: */
/* */
/* Return Value: none */
/*****************************************************************/
WordData& operator++(); // preincrement
WordData operator++(int); // postincrement
private: //PRIVATE VARIABLES
string word; //Object's word
int count; //Object's occurrence counter
};
解决方案
#include<string>
void menu(string,int);
int main()
{
//This problem here
menu("hello",1);
//............
return 0;
}
void menu(string wrd,int cnt)
{
std::cout << wrd << " " << cnt;
}
For a better answer poet the errors you are getting. Either from the compiler or from run time.