C++ 如何避免同一件东西被反复打印? 案例4: { 字符串书名; 国际图书展; 国际书评杂志; 浮动定价; 书名; cout>bookPageNDel; cout>bookReviewDel; cout>bookPriceDel; 对于(int i=0;i
仅当所有条目都不匹配时才打印“Book not existence”,而不是每次都不匹配时 因此,如果存在匹配项,则将布尔变量设置为true(和break),如果循环结束后没有匹配项,则打印 编辑:伪代码C++ 如何避免同一件东西被反复打印? 案例4: { 字符串书名; 国际图书展; 国际书评杂志; 浮动定价; 书名; cout>bookPageNDel; cout>bookReviewDel; cout>bookPriceDel; 对于(int i=0;i,c++,arrays,for-loop,C++,Arrays,For Loop,仅当所有条目都不匹配时才打印“Book not existence”,而不是每次都不匹配时 因此,如果存在匹配项,则将布尔变量设置为true(和break),如果循环结束后没有匹配项,则打印 编辑:伪代码 case 4: { string bookTitleDel; int bookPageNDel; int bookReviewDel; float bookPriceDel;
case 4:
{
string bookTitleDel;
int bookPageNDel;
int bookReviewDel;
float bookPriceDel;
cout << "\nPlease Enter the Title to be Deleted: ";
cin >> bookTitleDel;
cout << "\nTotal Number of Pages of the Book to be Deleted: ";
cin >> bookPageNDel;
cout << "\nPlease Enter Rating (stars): ";
cin >> bookReviewDel;
cout << "\nPlease Enter Price: ";
cin >> bookPriceDel;
for(int i=0;i<MAX_BOOKS;i++)
{
if((books[i].bookTitle!=bookTitleDel) && (books[i].bookPageN!=bookPageNDel) && (books[i].bookReview!=bookReviewDel) && (books[i].bookPrice!=bookPriceDel))
{
cout<<"\n\nBook Doesnt Exist\n";
continue;
}
}
for(int i=0; i<MAX_BOOKS; i++)
{
if((books[i].bookTitle==bookTitleDel) && (books[i].bookPageN==bookPageNDel) && (books[i].bookReview==bookReviewDel) && (books[i].bookPrice==bookPriceDel))
{
a=i;
books[i]= {};
cout << "\nBook Deleted\n";
for(int k=a; k<MAX_BOOKS-1; k++)
{
books[k]=books[k+1];
}
break;
}
}
break; //break to exit switch case #4.
boolean foundMatch=false;
对于(int i=0;i仅当所有条目都不匹配时才打印“Book not existence”,而不是每次都不匹配时
因此,如果存在匹配项,则将布尔变量设置为true(和break),如果循环结束后没有匹配项,则打印
编辑:伪代码
case 4:
{
string bookTitleDel;
int bookPageNDel;
int bookReviewDel;
float bookPriceDel;
cout << "\nPlease Enter the Title to be Deleted: ";
cin >> bookTitleDel;
cout << "\nTotal Number of Pages of the Book to be Deleted: ";
cin >> bookPageNDel;
cout << "\nPlease Enter Rating (stars): ";
cin >> bookReviewDel;
cout << "\nPlease Enter Price: ";
cin >> bookPriceDel;
for(int i=0;i<MAX_BOOKS;i++)
{
if((books[i].bookTitle!=bookTitleDel) && (books[i].bookPageN!=bookPageNDel) && (books[i].bookReview!=bookReviewDel) && (books[i].bookPrice!=bookPriceDel))
{
cout<<"\n\nBook Doesnt Exist\n";
continue;
}
}
for(int i=0; i<MAX_BOOKS; i++)
{
if((books[i].bookTitle==bookTitleDel) && (books[i].bookPageN==bookPageNDel) && (books[i].bookReview==bookReviewDel) && (books[i].bookPrice==bookPriceDel))
{
a=i;
books[i]= {};
cout << "\nBook Deleted\n";
for(int k=a; k<MAX_BOOKS-1; k++)
{
books[k]=books[k+1];
}
break;
}
}
break; //break to exit switch case #4.
boolean foundMatch=false;
对于(int i=0;i而不是2表示循环,使用1表示循环,并使用Patashu告诉的布尔变量
boolean foundMatch = false;
for(int i=0;i<MAX_BOOKS;i++)
{
if((books[i].bookTitle==bookTitleDel) && (books[i].bookPageN==bookPageNDel) && (books[i].bookReview!=bookReviewDel) && (books[i].bookPrice==bookPriceDel))
{
foundMatch = true;
break;
}
}
if (!foundMatch)
{
cout<<"\n\nBook Doesnt Exist\n";
}
switch=false
对于(int i=0;i而不是2表示循环,使用1表示循环,并使用Patashu告诉的布尔变量
boolean foundMatch = false;
for(int i=0;i<MAX_BOOKS;i++)
{
if((books[i].bookTitle==bookTitleDel) && (books[i].bookPageN==bookPageNDel) && (books[i].bookReview!=bookReviewDel) && (books[i].bookPrice==bookPriceDel))
{
foundMatch = true;
break;
}
}
if (!foundMatch)
{
cout<<"\n\nBook Doesnt Exist\n";
}
switch=false
对于(int i=0;这正是我正在尝试做的,但没有线索怎么办?请您帮助?@ RAPTZ我是C++的新手)“PATASU,我在我的圈子里放了一个计数器,但我从你的答案中得到了一个不同的方法,谢谢你,谢谢。我正想这么做,但我没有线索。你介意帮忙吗?@ RAPTZ我是C++的新手。@patashu我发现了一种不同的方法,在我的for循环中加入了一个计数器,但是我从你的答案中得到了这个想法,Thanx很多。