[英]I'm trying to call void functions in another function, but I don't know how to properly place them
[英]I don't know why this void function is giving me an error
案例 2 中的 function 由于某种原因无法正常工作。 预期的 output 说参数是未定义的,但是当我定义参数时,它说它只能是 integer 但是当我用变量替换字符串时,调试器仍然发出错误说“C++ 确实不支持默认 Int。” 我不断更改变量以适应调试的需要,但程序仍然无法运行。
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <fstream>
#include <time.h>
#include <stdio.h>
#include <Windows.h>//Secret to play music: Go to project file properties, go to linker and find input, in output write this Winmm.lib and press okay and done.
#include <mmsystem.h>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include <iterator>
#include <algorithm>
#include <deque>
void email(string emailbackup);
using std::cout;
using std::cin;
using namespace std;
int main()
{
int x;
std::cout << "What would you like to do?" << std::endl;
std::cout << "1)Would you like to see the archive?" << std::endl;
std::cout << "2)Would you like to register student information?" << std::endl;
std::cout << "3)Would you like to find a student within the registry?" << std::endl;
std::cout << "4)Delete all student information?" << std::endl;
std::cout << "5)Exit Program?" << std::endl;
std::cin >> x;
switch (x)
{
case 1:
break;
case 2:
email(emailbackup);//This is where the error is located within int main.//
//emailbackup is the parameter I am describing within int main()//
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
void email(string emailbackup)
{
string emailbackup;
int studentID = 0;
std::string let("abcdefghijklmnopqrstuvwxyz");
int numbers = (1, 3, 0);
std::string str;
std::string name;
std::string studID;
std::string studID2;
string email;
string studFinal;
std::stringstream concatenate;
std::stringstream concatenatetwo;
int amountofStudents;
std::cout << "How many student archives would you like to enter?" << std::endl;
std::cin >> amountofStudents;
amountofStudents + 1;
for (int i = 0; i < amountofStudents; i++)
{
cout << "Please enter your name.." << std::endl;
std::cin >> name;
while (std::string::npos != name.find_first_of("0123456789"))
{
cout << "You must have letter within user input." << std::endl;
cout << "Please enter your name." << std::endl;
std::cin >> name;
}
//I need to check to see if the first 3 numbers are correct?
//The student ID must be at least 6 digits, and the first 3 numbers must be 130.
cout << "Please enter Your student I.D." << std::endl;
std::cin >> studID;
stringstream(studID) >> studentID;
while (/*std::string::npos != studID.find_first_of("130") */ studentID != 130 /*&& studID.length() <= 6*/)
{
stringstream(studentID) >> studID;
cout << "You must enter 130 as the first 3 digits" << std::endl;
std::cin >> studID;
stringstream(studID) >> studentID;
}
//==============
cout << "Please enter the second part of the student I.D. " << studentID << "-" << std::endl;
std::cin >> studID2;
while (/*std::string::npos != studID.find_first_of("130") */ studID2.length() < 3 || studID2.length() > 3)
{
//stringstream(studentID) >> studID;
cout << "Add 3 more digits." << std::endl;
std::cin >> studID2;
}
concatenate << studentID << "-" << studID2 << endl;
studFinal = concatenate.str();
/*while (studID.length() != 6)
{
cout << "You must enter 130 as the first 3 digits and you must have 6 digits." << std::endl;
std::cin >> studID;
}*/
cout << "Please enter your email.." << endl;
cin >> email;
while (/*string::npos != email.find_first_not_of("@atlanticu.edu")*/ email == emailbackup || email.empty())
{
cout << "Please enter your email..." << endl;
cin >> email;
}
concatenatetwo << email << "@atlanticu.edu" << endl;
email = concatenatetwo.str();
emailbackup = email;
cout << "Your email is.. " << email << endl;
system("pause");
}
}
我只需要知道如何修复 function 参数,这样我就可以正确执行程序。
想想你的 function 签名。
void email(string emailbackup);
您是否真的希望调用者将字符串传递给 function email()
?
因为您随后在 function 中再次定义 emailbackup。
你可能想要这样的东西......
...
case 2:
const string backup = "backup@email";
email(backup);
break;
...
void email(string emailbackup)
{
// string emailbackup; // delete this line
...
}
另外,删除using namespace std;
并与您的命名空间使用保持一致。 为什么要添加using namespace cout, cin
等,如果您使用命名空间显式调用它们,即 std::cin、std::cout。
添加using namespace std::cout
并使用cout
而不是std::cout
,反之亦然。 不要混淆它。 与std::string
和string
相同。 让它看起来像你定义了自己的字符串......
最后一件事,为了便于阅读,我建议将这个 email function 分解为更小的函数。 例如:
void email(string emailbackup)
{
...
int studentID = getStudentID();
string studentEmail = getStudentEmail();
...
}
int getStudentID()
{
int studentID;
cout << "Please enter Your student I.D." << std::endl;
std::cin >> studID;
stringstream(studID) >> studentID;
while (/*std::string::npos != studID.find_first_of("130") */ studentID != 130 /*&& studID.length() <= 6*/)
{
stringstream(studentID) >> studID;
cout << "You must enter 130 as the first 3 digits" << std::endl;
std::cin >> studID;
stringstream(studID) >> studentID;
}
return studentID;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.