输出不会使用方法和结构产生
我使用方法,数组和结构尝试此代码,但根本不会产生输出。
我还使用大多数类型的包括,但仍然不会产生任何输出。
#include<conio.h>
#include<iostream>
#include<stream>
#include<string>
#include<stream>
using namespace std;
struct PetLabel {
string petname;
string pettype;
int petprice;
string petowner;
string phoneno;
};
PetLabel pet [10];
stringstream ss;
void DataPet();
void petPrice();
void findOwner();
int main(){
DataPet();
findOwner();
petPrice();
return 0;
}
void DataPet(){
ifstream infile;
infile.open("petSold.txt");
string cnvt[10];
for(int i = 0; i < 10; i++){
getline(infile, pet[i].petname, ',');
getline(infile, pet[i].pettype, ',');
getline(infile, cnvt[i], ',');
ss << cnvt[i];
ss >> pet[i].petprice;
getline(infile, pet[i].petowner, ',');
getline(infile, pet[i].phoneno);
ss.clear();
}
infile.close();
}
void findOwner(){
int chosen;
for (int i = 0; i < 10; i++){
if (pet[i].petname == "Uyasolu"){
i = chosen;
}
}
cout << "Owner name : " << pet[chosen].petowner << " Phone no : " << pet[chosen].phoneno << endl;
}
void petPrice(){
ofstream outfile("catSold.txt");
outfile << "The cat sold with price greater than RM1000" << endl;
for (int i = 0; i < 10; i++){
if (pet[i].petprice > 1000){
cout << pet[i].petname << "," << pet[i].pettype << "," << pet[i].petprice << "," << pet[i].petowner << "," << pet[i].phoneno << endl;
}
}
outfile.close();
}
我得到的输出是:
Owner name : Phone no :
但是我无法理解,因为根本没有语法错误。
我可以添加什么,所以我可以得到正确的输出?
I try this code using a method, array, and struct, but it doesn't produce output at all.
I also use most types of include, but it still doesn't produce any output.
#include<conio.h>
#include<iostream>
#include<stream>
#include<string>
#include<stream>
using namespace std;
struct PetLabel {
string petname;
string pettype;
int petprice;
string petowner;
string phoneno;
};
PetLabel pet [10];
stringstream ss;
void DataPet();
void petPrice();
void findOwner();
int main(){
DataPet();
findOwner();
petPrice();
return 0;
}
void DataPet(){
ifstream infile;
infile.open("petSold.txt");
string cnvt[10];
for(int i = 0; i < 10; i++){
getline(infile, pet[i].petname, ',');
getline(infile, pet[i].pettype, ',');
getline(infile, cnvt[i], ',');
ss << cnvt[i];
ss >> pet[i].petprice;
getline(infile, pet[i].petowner, ',');
getline(infile, pet[i].phoneno);
ss.clear();
}
infile.close();
}
void findOwner(){
int chosen;
for (int i = 0; i < 10; i++){
if (pet[i].petname == "Uyasolu"){
i = chosen;
}
}
cout << "Owner name : " << pet[chosen].petowner << " Phone no : " << pet[chosen].phoneno << endl;
}
void petPrice(){
ofstream outfile("catSold.txt");
outfile << "The cat sold with price greater than RM1000" << endl;
for (int i = 0; i < 10; i++){
if (pet[i].petprice > 1000){
cout << pet[i].petname << "," << pet[i].pettype << "," << pet[i].petprice << "," << pet[i].petowner << "," << pet[i].phoneno << endl;
}
}
outfile.close();
}
the output that I get is:
Owner name : Phone no :
but I can't understand, because there is no syntax error at all.
Is there anything I can add, so I can get the right output?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
此方法很奇怪:
我认为您的意思是
选择= i
。This method is weird:
I think you mean
chosen = i
instead.findowner()
编码错误。int选择;
在输入循环之前是不可原始化的。在循环中,选择
永远不会更新(i
已更新,这会影响循环)。循环完成后,选择
具有不确定的值,因此访问pet [选择]
is 不确定的行为 。它应该更像是这样:
或者
:
也不使用
&lt; conio.h&gt;
,它的旧,您没有使用,您也没有使用无论如何,任何东西。您有
#include&lt; stream&gt;
两次,以及&lt; stream&gt;
应该是&lt; sstream&gt;
#include& lt [10];
应为字符串CNVT;
,然后cnvt [i]
应该为cnvt
。一次仅转换1字符串
时,您不需要整个数组。ss.clear();
不做您认为的事情。您需要改用ss.str(“”)
。或者,只需将ss
的声明作为局部变量进入循环中,以便在每个循环迭代中被破坏并重新创建。petprice()
正在创建输出catsold.txt
文件,但没有写任何东西。findOwner()
is coded wrong.int chosen;
is uninitialized before the loop is entered. Inside the loop,chosen
is never updated (i
is updated instead, which affects the looping). After the loop is done,chosen
has an indeterminate value, so accessingpet[chosen]
is undefined behavior.It should look more like this instead:
Alternatively:
There are some other issues in your code, too:
don't use
<conio.h>
, its old, and you are not using anything from it anyway.you have
#include<stream>
twice, and also<stream>
should be<sstream>
string cnvt[10];
should bestring cnvt;
and thencnvt[i]
should becnvt
. You don't need a whole array when you are converting only 1string
at a time.ss.clear();
does not do what you think it does. You need to usess.str("")
instead. Or, simply move the declaration ofss
inside the loop as a local variable so it is destroyed and recreated on each loop iteration.petPrice()
is creating an outputcatSold.txt
file, but is not writing anything to it.