小编log*_*_92的帖子

c ++ do while 条件未按预期工作

我想知道为什么while (selection != 'q' && selection != 'Q')有效但while (selection != 'q' || selection != 'Q')不起作用。它永远不会终止循环。当我使用else if (selection == 'q' || selection == 'Q' )(with ||) 时,它工作正常。有人可以帮忙吗?

#include <iostream>

using namespace std;

int main()
{
    char selection{};
    do{
        cout << "\n--------------------------"<< endl;
        cout << "1.Do this" << endl;
        cout << "2.Do that" << endl;
        cout << "3.Do something else" << endl;
        cout << "4.Quit" << endl;
        cout << "\nEnter your selection" << endl;
        cin …
Run Code Online (Sandbox Code Playgroud)

c++

3
推荐指数
1
解决办法
54
查看次数

x=10 &amp; x{10} 有什么区别?

我是编程新手,我看到了 2 种类型的变量初始化。int x=10 && int x{10} 和有什么不一样?在这种情况下 int 是一个类而 x 是一个对象吗?

#include <iostream>
using namespace std;
int main() {
    int x=10;
    int y{10};
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

2
推荐指数
1
解决办法
72
查看次数

cout of string 不显示任何内容

encrypted字符串的 cout不显示任何内容,有时程序会崩溃。当我做cout << encrypted[i]for 循环时,我得到了正确的结果。此外,如果我执行 for 循环以按字符读取字符串字符for(char c:encrypted)cout << c << endl;=> 它也不起作用并且得到了垃圾。

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

int main()
{
    string alphabet = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    string key = "XZNLWEBGJHQDYVTKFUOMPCIASRxznlwebgjhqdyvtkfuompciasr";
    string encrypted;
    string decrypted;
    string message;
    int pos{};
    cout << "enter the message" << endl;
    getline(cin,message);


    //encrypting
    for (size_t i{} ;i<message.length();i++)
        {   
            if (alphabet.find(message[i]) != string::npos)
            {pos = alphabet.find(message[i]);        

            encrypted[i] = key[pos];                    

            }else
             {encrypted[i]=message[i];
                cout << encrypted[i];
             }
        }

    cout << "the …
Run Code Online (Sandbox Code Playgroud)

c++ string

0
推荐指数
1
解决办法
68
查看次数

标签 统计

c++ ×3

string ×1