`const char*'到`char'

Ben*_*ton 0 c++ string comparison pointers countdown

我是编程新手,并试图改进我的基本倒数计时器.我不知道为什么我收到此错误,其他问题在不同情况下,因此不适合我的程序.

//countdown timer using while loops, if else, strings and sleep

#include <iostream>
#include <windows.h>
#include <string>
using namespace std;

int main ()
{
    char progend[5];
    float a; /* a will be floating point */
    cout << "Enter start the the number you want to count down from" << ".\n";

    while (a>-1) { /* the main program is located here */

        cin >> progend[5];

        if (progend[5] = "end") /* if the user inputs end the program ends */
        {
            a = -1;
        }

        else if (progend [5] = "start")
        {
            cin >> a;
            while (a>0) { /* the actual countdown timer*/
                Sleep(100);
                a = a - 0.1;
                cout << a;
            }

            cout << "Finished!" << ".\n" << "Enter start then enter another number to count down                 from or enter end to close the program" << ".\n";
        }

        else
        {
            cout << "Enter yes or end";
        }

    }
return 0;
}
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.

Lih*_*ihO 5

char progend[5];
...
if (progend [5] = "start")
Run Code Online (Sandbox Code Playgroud)

尝试将字符串文字分配给数组的"start"第6个字符progend(甚至不存在).请注意,即使此代码尝试分配字符,在结束后写入数组也会导致未定义的行为.

你可以使用C风格strcmp:

if (strcmp(progend, "start") == 0)
Run Code Online (Sandbox Code Playgroud)

或者甚至更好:因为这是C++,所以std::string改为使用对象:

std::string progend;
...
if (progend == "start") ...      // <-- this will use std::string::operator==
Run Code Online (Sandbox Code Playgroud)