我在c++应用程序上搞不清楚
I cant figure it out something on a c++ application
所以我正在制作一个c++应用程序来解决一些练习。
int i=1;
cout << "How many times :";
cin >> n;
while (n>0)
{
cout << "Input F" << i << ":";
if (cin >> a[i] && cin >> sign&& cin>>b)
{
switch (sign)
{
case'$': a[i] = a[i] * (sqrt(b));
break;
case'^': a[i] = pow(a[i], b);
break;
case'/':a[i] = a[i] / b;
break;
default: cout << "n Wrong sign";
break;
}
}
else
a[i]=a[i];
i++;
n--;
}
在某些情况下,我不需要sing或b变量。。。当我按Enter键时,应用程序不允许我退出if。
例如:我必须输入F1:8sqrt(2),然后输入8$2,然后输入F2:just 8,然后按Enter键,但应用程序一直在询问值。
好的,现在为了让它发挥作用,我需要连续键入两次符号。。。
while (n>0)
{
cout << "Input F" << i << ":";
cin >> a[i];
if (cin.get() == 'n')
a[i] = a[i];
else {
cin >> sign;
if (sign)
cin >> b;
switch (sign)
{
case'$':
a[i] = a[i] * (sqrt(b));
break;
case'^': a[i] = pow(a[i], b);
break;
case'/':a[i] = a[i] / b;
break;
default: cout << "n Wrong sing;
break;
}
}
i++;
n--;
}
谢谢你抽出时间。
你说:
在某些情况下,我不需要sing或b变量。。。当我按Enter键时,应用程序不允许我退出if。
这是因为程序正在等待您输入所需的所有值
if (cin >> a[i] && cin >> sign&& cin>>b)
您可以通过输入所有必要的输入或输入EOF(输入EOF是特定于平台的)来摆脱该语句。
通过逐行读取输入并使用std::istringstream
独立处理每一行,您会过得更好。
while (n>0)
{
std::string line;
cout << "Input F" << i << ":";
if ( ! getline(cin, line) )
{
// Error reading the next line.
break;
}
// Now extract the data from the line using a istringstream.
std::istringstream str(line);
if (str >> a[i] && str >> sign && str >> b)
{
switch (sign)
{
case'$': a[i] = a[i] * (sqrt(b));
break;
case'^': a[i] = pow(a[i], b);
break;
case'/': a[i] = a[i] / b;
break;
default: cout << "n Wrong sign";
break;
}
}
else
a[i]=a[i];
i++;
n--;
}
相关文章:
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 关于隐式声明的复制构造函数的引用在逻辑上不清楚
- 视窗 10 :笔(手写笔)在 MFC 应用程序上不起作用
- 多线程程序中的分段故障和gdb回溯上的不完整信息
- 为什么关闭初始程序后在ncat上看不到UDP?
- ShowCursor(FALSE) 不会在控制台应用程序上隐藏光标
- C++程序在VS上运行平稳,但在Linux Mint上运行不畅
- 我不知道为什么调试断言在我的程序上失败的答案
- 在 MSVC 中将某些库的目标平台版本设置为 10.x 是否使程序与在 Windows Vista/7/8 上运行不兼容
- 在笔记本电脑上开发不支持 GPU 的 OpenCL 应用程序
- OSX 上的应用程序构建找不到配置文件
- 无法弄清楚为什么我的程序在条件不为真时执行 if 语句(数组)
- 我在c++应用程序上搞不清楚
- 有没有一种方法可以在一个平台上编写c++程序,而不需要为不同的操作系统重新编写
- 不会进入控制台应用程序上的 Windows GetMessage 循环
- 我在程序上找不到错误 (C++)
- 为什么我会在应用程序上出现未知C++参考链接错误,而不是 Linux 中的库
- 如何在GPU上而不是在CPU上运行qt应用程序
- 尝试在此测试分数程序上使用指针表示法而不是数组表示法,但我被困住了
- /usr/bin/ld:在Linux Mint上的Qt Creator中构建Qt5应用程序时找不到lGL