如果在数组中找到元素,则打印整行
Print entire row if element is found in an Array
如何搜索数组中的元素并打印它的整行?我一直在做这样的变通方法,但似乎无法输出我期望的结果。
#include <iostream>
#include <stdlib.h>
#include <string>
#include <ctype.h>
#include <sstream>
using namespace std;
int main() {
string items[9][3] = {{"A","BALOT","25.00"},
{"B","CANTON","20.00"},
{"C","NIDO","100.00"},
{"D","KETCHUP","50.00"},
{"E","MAGGI","15.00"},
{"F","ALASKA","60.00"},
{"G","VINEGAR","25.00"},
{"H","OIL","70.00"},
{"I","COKE","10.00"}};
// PARA SA MAPRINT YUNG ARRAY.
cout << "MANG JUAN'S 10-DAHANnn";
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 3; j++)
cout << items[i][j] << ( (j < 2) ? "-" : "t" );
if (i < 6) {
cout << "t";
i += 2;
}
else if (i != 8) {
cout << "n";
i -= 6;
}
} // END OF ARRAY PRINTING
char choice;
int ctr = 1, quantity;
string purchased;
cout << "nnWOULD YOU LIKE TO PURCHASE? Y/Nnn";
cin >> choice;
if(choice == 'n' || choice == 'N') {
cout << "Thank you. ";
}
else if(choice == 'y' || choice == 'Y') {
string numPref;
while (true) {
if(ctr > 11) {
cout << "nnTHE SYSTEM EXCEEDED ITS LIMITnn";
break;
} else {
if(ctr == 1) numPref = "st";
else if(ctr == 2) numPref = "nd";
else if(ctr == 3) numPref = "rd";
else if(ctr > 3) numPref = "th";
}
cout << "nnPLEASE ENTER " << ctr << numPref << " ITEM:t";
cin >> purchased;
if(!cin) {
cout << "Letters only";
break;
} else {
if(true) {
cout << "HOW MANY? ";
cin >> quantity;
if(!cin) {
cout << "Enter number only. ";
break;
} else {
cout << "PRICE PER ITEM: ";
///////// Look for the element and print the entire row /////////////
string matchedRow[3];
for (int i = 0; i < 3; i++) {
string oneRow[] = items[i];
if (oneRow[0] == purchased) {
matchedRow = oneRow;
break;
}
}
for (int i = 0; i < matchedRow.length; i++) {
cout << matchedRow[i] + "tt";
}
////////////////////////////////////////////
}
ctr++;
} // end of else - if (!cin) for quantity input check
} // end of char check
} // End of else for (!cin)
} // End of while loop for numPref
} // End of else if (choice)
system("PAUSE");
return 0;
}
示例:如果用户在Please enter item
上输入A
,程序将在数组上输出Price per item
和相应的价格。
Sample Run:
lease enter item: A // user input
How many? 3 // user input
Price per item: 25.00 // not user input
使用指针复制一行:
string *oneRow = items[i];
然后您可以访问价格为:
oneRow[2]
修改后的程序:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <ctype.h>
#include <sstream>
using namespace std;
int main() {
string items[9][3] = {{"A","BALOT","25.00"},
{"B","CANTON","20.00"},
{"C","NIDO","100.00"},
{"D","KETCHUP","50.00"},
{"E","MAGGI","15.00"},
{"F","ALASKA","60.00"},
{"G","VINEGAR","25.00"},
{"H","OIL","70.00"},
{"I","COKE","10.00"}};
// PARA SA MAPRINT YUNG ARRAY.
cout << "MANG JUAN'S 10-DAHANnn";
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 3; j++)
cout << items[i][j] << ( (j < 2) ? "-" : "t" );
if (i < 6) {
cout << "t";
i += 2;
}
else if (i != 8) {
cout << "n";
i -= 6;
}
} // END OF ARRAY PRINTING
char choice;
int ctr = 1, quantity;
string purchased;
cout << "nnWOULD YOU LIKE TO PURCHASE? Y/Nnn";
cin >> choice;
if(choice == 'n' || choice == 'N') {
cout << "Thank you. ";
}
else if(choice == 'y' || choice == 'Y') {
string numPref;
while (true) {
if(ctr > 11) {
cout << "nnTHE SYSTEM EXCEEDED ITS LIMITnn";
break;
} else {
if(ctr == 1) numPref = "st";
else if(ctr == 2) numPref = "nd";
else if(ctr == 3) numPref = "rd";
else if(ctr > 3) numPref = "th";
}
cout << "nnPLEASE ENTER " << ctr << numPref << " ITEM:t";
cin >> purchased;
if(!cin) {
cout << "Letters only";
break;
} else {
if(true) {
cout << "HOW MANY? ";
cin >> quantity;
if(!cin) {
cout << "Enter number only. ";
break;
} else {
cout << "PRICE PER ITEM: ";
///////// Look for the element and print the entire row /////////////
string *matchedRow;
const int length = 3;
for (int i = 0; i < 3; i++) {
string *oneRow = items[i];
if (oneRow[0] == purchased) {
matchedRow = oneRow;
cout << matchedRow[2];
break;
}
}
//you don't need this
/*
for (int i = 0; i < length; i++) {
cout << matchedRow[i] + "tt";
}
*/
////////////////////////////////////////////
}
ctr++;
} // end of else - if (!cin) for quantity input check
} // end of char check
//} // End of else for (!cin) //spare bracket
} // End of while loop for numPref
} // End of else if (choice)
//system("PAUSE");
//return 0;
}
#include <algorithm>
#include <iostream>
int main() {
// Keep it sorted!
std::string items[9][3] = {
{"A","BALOT","25.00"},
{"B","CANTON","20.00"},
{"C","NIDO","100.00"},
{"D","KETCHUP","50.00"},
{"E","MAGGI","15.00"},
{"F","ALASKA","60.00"},
{"G","VINEGAR","25.00"},
{"H","OIL","70.00"},
{"I","COKE","10.00"}};
typedef std::string Item[3];
Item* last_item = items + sizeof(items) / sizeof(Item);
struct Less {
bool operator () (const Item& a, const std::string& b) const {
return a[0] < b;
}
};
std::string in;
std::cin >> in;
Less less_item;
Item* item = std::lower_bound(items, last_item, in, less_item);
if(item == last_item || (*item)[0] != in) std::cout << "Not found" << std::endl;
else std::cout << "Found: " << (*item)[0] << ": " << (*item)[1] << ", Price = " << (*item)[2] << std::endl;
return 0;
}
在您定义字符串oneRow[]的行中,我认为它应该是
string oneRow[3] = {"Z","WATER","00.00"}; // default initialization
oneRow[0] = items[i][0];
oneRow[1] = items[i][1];
oneRow[2] = items[i][2];
相关文章:
- 从控制台中删除最后打印的元素
- 数组元素打印的递归方法
- 打印所有链表的元素 (C++)
- 更新的矢量元素不打印
- 打印矢量数组中的所有元素
- 为什么我在尝试在单向链表中打印元素时会出现这个永无止境的循环
- 如何在c ++中打印给定桶号的所有元素unordered_set?
- 为什么平均打印数组元素比打印单个对象慢C++?
- 为什么这个程序没有打印返回的迭代器的正确第二个元素?
- C++ Expat 仅打印元素的第一个字母和标签中的数据
- 如何在 2D 字符数组中打印 [x][y] 元素的地址?(C++)
- C 打印元素总和
- C 打印元素,带有来自对象数组的数据
- 将数组传递到空隙函数,然后递归打印元素
- 使用std :: lower_bound()打印元素范围以下某些值以下
- 3D 指针数组的打印元素
- 如何打印元素的地图内部的矢量
- 中缀,前缀或后缀顺序通过BST得到降序打印元素
- 从列表中打印元素
- 如何在矢量 c++ 中打印元素