检查选定的数组是否存储了任何内容?C
Checking to see if the selected array has anything stored in it? C++
#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
using namespace std;
int main()
{
int i;
string information[10][7];
//This bit should check if theres anything stored currently.
cout << "nWhich Slot would you like to store the informaton in ?(1-10)";
cin >> i;
i--;
//input
for (int j=0;j<7;j++)
{
switch(j+1)
{
case 1:
cout << "nFirst Name: ";
break;
case 2:
cout << "nLast Name: ";
break;
case 3:
cout << "nAge: ";
break;
case 4:
cout << "nEmail: ";
break;
case 5:
cout << "nDoor Number: ";
break;
case 6:
cout << "nRoad Name: ";
break;
case 7:
cout << "nPost Code: ";
break;
default:
;
}
cin >> information[i][j];
}
// output
for (int j=0;j<7;j++)
{
switch(j+1)
{
case 1:
cout << "nFirst Name: ";
break;
case 2:
cout << "nLast Name: ";
break;
case 3:
cout << "nAge: ";
break;
case 4:
cout << "nEmail: ";
break;
case 5:
cout << "nDoor Number: ";
break;
case 6:
cout << "nRoad Name: ";
break;
case 7:
cout << "nPost Code: ";
break;
default:
;
}
cout << information[i][j];
}
system("PAUSE");
return 0;
}
基本上,问题是我对这一点代码做什么,以检查用户选择的数组是否已经存储了信息?我该如何检查。另外,我想知道如何通过输入年龄来搜索用户的详细信息,例如即使我使用的字符串是可能的,或者我必须使用int。?
使用布尔数组可能会产生一些开销,但是如果您也执行删除也可能会有所帮助:
bool stored[10] = {false};
/* ... */
do {
cout << "nWhich Slot would you like to store the informaton in ?(1-10)";
cin >> i;
i--;
} while (stored[i]);
stored[i] = true;
当然,您可以定义一个具有7个字符串成员的类/结构,其中一个布尔值用于此信息。
让我们从代码看起来好像返回到弗隆的事实首先,您有十个字段,有7个Charcters长。即使我们四处互换,有10个字符的名称或电子邮件地址也很短。我的姓是9个字母,我看到的名字比那更长。
,最后是您的问题。您可以使用Perreal建议的方法。但是我可能只是做if (string[i][0] == "") ... stuff to do when it's empty ...;
。
使用string::length
函数(information[1].length()
)怎么样?如果返回0,则尚未初始化。
只是要找到一些东西。
相关文章:
- 如何在共享库的整个生命周期内存储数据
- 是否有任何内置的哈希图函数来处理 c++ 中的值输入?
- 是否有任何内置函数可以检查给定的两个数字在给定整数数组中的顺序是否相同?
- C++ 在节点内存储对数据对象的引用,而不复制数据
- 将类的任何模板存储在另一个类中
- 无法将任何内容存储到我的数组中
- C++ 中是否有任何内置阶乘函数?
- 如何在双精度内存储时间
- 是否有任何内置函数可以告诉编译器分支是否可预测
- 我需要在C /C#中使用图形.我可以使用任何内置的C /C#库
- 是否有任何内置类可以处理C 中的大整数值
- c ++中是否有任何内置功能可以在计划的时间运行任务
- 是否有任何内置的 UTF 在 c++ 在窗口中
- 在VC++2010中执行任何内置的ARRAY函数
- C++:如何在不使用任何内置函数的情况下将字符串转换为整数
- 为什么我得到一个bps_remove_fd失败时,试图在QScopedPointer内存储一个QSslSocket
- 是否有任何内置的CUDA函数允许CUDA内核向主机代码报告错误?
- 使用box.net, dropbox,(或任何云存储)与一个c++应用程序
- 我如何在常数时间(O(1))内获得字母表(1-26)中字符的数值/位置,而不使用任何内置方法或函数
- C++中的任何内置哈希方法