繁体   English   中英

添加简单的cout后程序将无法运行

[英]Program won't run after adding simple cout

尝试(返回)C ++并通过书中的练习进行操作时遇到一个奇怪的错误。

使用代码:: blocks + GCC

这是当前程序。

    #include "std_lib_facilities.h";
    int main()
    {

    double val, smallest = 9999, largest = 0;
    string strunit;
        for (int i = 0; i < 10; i++) {
            cout << "Enter value: ";
            cin >> val >> strunit;

            if (strunit == "in") {
                    val = val*0.254;
            } else if (strunit == "cm") {
                val = val/100;
            }
            if ( val > largest) {
                largest = val;
            } else if (val < smallest) {
                smallest = val;
            }
            cout << "Largest: " << largest << "smallest: " << smallest << endl;

        }

   return 0;
}

我打算使用while循环,但现在是一个循环。 结果是一样的。

这是构建结果:

-------------- Build: Debug in drill_4 (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe  -o bin\Debug\drill_4.exe obj\Debug\exe\principles_and_practices\drill_4\main.o   
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

IDE声称该程序尚未生成,并要求我重新生成。

如果我几乎注释掉任何一行,程序将运行。

我最初使用if语句一行,结果相同。

我对IDE也很陌生,所以不确定在哪里可以找到我需要的所有信息,但是我想Pgrogram无法正确链接?

知道我在做什么错吗?

尝试使用#include<iostream>using namespace std;

在我输入以下内容时为我工作:

#include <iostream>
#include<string>
using namespace std;


int main()
{

double val, smallest = 9999, largest = 0;
string strunit;
    for (int i = 0; i < 10; i++) {
        cout << "Enter value: ";
        cin >> val >> strunit;

        if (strunit == "in") {
                val = val*0.254;
        } else if (strunit == "cm") {
            val = val/100;
        }
        if ( val > largest) {
            largest = val;
        } else if (val < smallest) {
            smallest = val;
        }
        cout << "Largest: " << largest << "smallest: " << smallest << endl;

    }
    system("pause");
}

由于某种原因,它不喜欢包含该标头。.您可能需要使用VS下载一些额外的文件才能使用它? 您正在使用Visual Studios吗?

cout在iostream头文件中的命名空间std之后。

编译器对此不知道cout或cin的对象实例。

您也可以添加标题并使用std :: cout。

#include <iostream>

.....
std::cout << "hellow world";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM