簡體   English   中英

C ++數組cin循環

[英]C++ array cin loop

我正在嘗試學習C ++,我是新手。 我有一個小數組程序,老師正在讓我寫。 他希望通過菜單選擇多個陣列。 我唯一的問題是我在幾個for循環中遇到問題。 選項一和二讓我滿意,但是我看起來cin不能正常工作或發生什么。

#include<iostream>
#include<string>

using namespace std;

int main()
{
    int Array1[5];
    int Array2[2];
    int Array3[4];
    int choice1;
    int choice2;


    cout << "******************************************" << endl;
    cout << "*  this damn thing has killed me today   *" << endl;
    cout << "******************************************\n\n\n\n" << endl;
    cout << "    Here is some options  \n\n" << endl;
    cout << "      1) start array of 5\n " << endl;
    cout << "      2) Make another array. Choices are  2/4" << endl;
    cout << "      3) Exit" << endl;
    cin >> choice1;

    if (choice1 == 1)
    {
        for (int a = 0; a < 5; ++a)
        {
            cout << "Lets start this" << endl;
            int y = 0;
            int n = 0;

            int x;
            cin >> x;
            Array1[a] = x;

            cout << " Wann see the numbers you entered??  Y/N" << endl;
            int question;
            cin >> question;
            if (question == y)
            {
                cout << Array1[0];
                cout << Array1[1];
                cout << Array1[2];
                cout << Array1[3];
                cout << Array1[4];
                cout << "n\n\\n" << endl;
                cout << "Bye Bye" << endl;
            }
            else if (question == n)
            {
                cout << "\n\n Bye Bye" << endl;
                return 0;
            }
        }
    }
    if (choice1 == 2)
    {
        cout << "Which Array Shall We DO??????\n1)2?\n2)4?\n" << endl;
        cin >> choice2;
        if (choice2 == 1)
            for (int c = 0; c < 2; ++c)
            {
                int z = 0;
                Array2[c] = z;
                cin >> z;

                cout << "Would you like to see the Array of 2? y/n?  " << endl;
                int q1;
                int y = 0;
                int n = 0;
                cin >> q1;

                if (q1 == y)
                {
                    cout << Array2[0];
                    cout << Array2[1];

                    cout << "\n\n\nBye Bye" << endl;
                    return 0;
                }

                if (q1 = n)
                {
                    cout << "\n\nBye Bye" << endl;
                }

            }
        if (choice2 = 2)
        {
            for (int d = 0; d < 4; ++d)
            {
                int y = 0;
                int n = 0;
                int w = 0;
                int q2;
                Array3[d] = w;
                cout << "Enter Numbers........\n\n" << endl;
                cin >> w;

                cout << "Would You Like to See Array?????    y/m\n\n" << endl;
                cin >> q2;

                if (q2 == y)
                {
                    cout << Array3[0];
                    cout << Array3[1];
                    cout << Array3[2];
                    cout << Array3[3];
                }

                if (q2 == n)
                {
                    cout << "\n\n\nBye Bye! " << endl;
                    return 0;
                }
            }
        }

    }
    if (choice1 == 3)
    {
        cout << "Bye Bye" << endl;
        return 0;
    }
}

所以首先,這里有很多錯誤。 由於您是編程的新手,所以我想為您提供幫助。 我不想做你的家庭作業,但我將為您提供足夠的信息,以使其盡快完成。

您的老師向您介紹了switch語句嗎? 如果是這樣,這是使用它的好時機。 無論如何,我們將留在這里。

因此,讓我們看一下各節中的代碼

 if (choice1 == 1)
{
    for (int a = 0; a < 5; ++a)
    {
        cout << "Lets start this" << endl;
        int y = 0;
        int n = 0;

        int x;
        cin >> x;
        Array1[a] = x;

這是怎么回事 首先,我想讓您知道這輸出給用戶

Lets start this

用戶和您一樣可能感到困惑。 但是我知道您想要一個數字,所以我們鍵入2。

下一部分

cout << " Wann see the numbers you entered??  Y/N" << endl;
            int question;
            cin >> question;
            if (question == y)

我在這里看到三個問題。 首先,我問你字母Y是什么類型? 它是一個角色。 不是整數。 其次,如果您的用戶鍵入小寫的y,會發生什么? 您還應該處理向用戶提出的問題。 請記住,用戶很笨,不知道您在做什么。 第三,嘗試if (question == 'y')

因此,現在在解決此問題之后,讓我們重新編譯,看看會發生什么? 我們再也不會中斷了,但是我們突然陷入了一個循環。 我們也打印出奇怪的數字。 那為什么呢? 讓我們將第8行更改為int Array1[5] = {0,0,0,0,0}; 我為什么要這樣做? 查看在打印語句時是否注意到發生的情況的模式。 有一個簡單的解決方法,但這是格式和樣式。

您的最后一個問題是您的else語句。 如果為什么還要使用其他? 為什么不呢? 為什么返回0? 如果用戶回答“否”,是否要退出該程序? 注釋掉整個部分,看看會發生什么。

您的其他問題與上述問題類似。 我強烈建議三件事。

1)編寫偽代碼。 我知道這是一個痛苦和煩人的事情,但是並不需要很長時間,並且可以節省您調試的時間。 它會在這里解決您的問題。

2)確保您要問用戶的問題清楚。

3)您的用戶很笨,您的老師在故意測試您的代碼時可能會很笨。 提出類似的問題,例如“如果用戶用我沒有解釋的內容做出響應,該怎么辦?就像在應該放置字符的地方放置一個數字。如果那么,那么else語句就很適合。那就是那樣。確保使用OR語句(|| )以考慮大寫字母和小寫字母。如果您想成為一個真正的好人,請弄清楚如何為輸入“ Y”,“ Yes”或“ yes”而傳遞“ y”。

希望對您有幫助。 經過這些修復,代碼便可以編譯並正常運行。 如果仍然不能解決所有問題,我將丟棄更多提示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM