【问题标题】:finding out where to put [I] within my program找出在我的程序中放置 [I] 的位置
【发布时间】:2020-05-25 18:49:19
【问题描述】:

下面我在程序中设置一个y/n 问题,我知道我需要一个名为i 的标识符,以便cin 行在if 中工作。但是,我不确定将我的标识符放在哪里。

#include <iostream>
#include <string>

using std::string, std::cin, std::cout, std::endl;

int main() {
    const int MaxNum = 3;
    int simcard;
    string familyone[MaxNum] = {
        "Name One",
        "Name Two",
        "Name Three"
    };
    int famnumone[MaxNum];
    int choice;
    string familytwo[MaxNum] = {
        "Name One",
        "Name Two",
        "Name Three"
    };
    int famnumtwo[MaxNum];
    string familythree[MaxNum] = {
        "Name One",
        "Name Two",
        "Name Three"
    };
    int famnumthree[MaxNum];
    string phonetype[MaxNum] = {
        "Iphone",
        "Android",
        "Random Phone"
    };
    string familysim[MaxNum] = {
        "Family one Sim",
        "Family two Sim",
        "Family Three Sim"
    };
    string simtype[MaxNum] = {
        "Iphone SIM",
        "Android SIM",
        "Random Phone SIM"
    };

    //Here we have information for each family members name to later identify who each phone belongs too

    cout << "SIM card matching tool\n";
    cout << "Enter Family One Names ==>";
    for (int i = 0; i < MaxNum; i++) {
        getline(cin, familyone[i]);
    }
    do {
        cout << "would you like to continue to the sim/y, or would you like to add another family/n" << endl;
        cin >> choice;
        choice = tolower(choice);
    } while (choice != 'n' && choice != 'y');

    if (choice == 'n') {
        cin >> familytwo[i];
        if (choice == 'y')
            cin >> simtype[i];
    }

    //add y/n below or possibly add a more complex anwser "if you would like  to continue with the SIM type SIM or if you would like to add another family type family
    cout << "Would you like to continue to the SIM/y, or would you like to add another family/n";
}

【问题讨论】:

  • 什么是 [I] 标识符?你的程序有错误吗?它会给出错误的结果吗?
  • 修正缩进。它现在相当混乱且难以阅读。你也错过了#includes。
  • 对您的问题更准确一点?
  • 在 do-while 循环之后,大括号的位置错误。如果选择是'n'`,那么检查选择是否是'y'是没有意义的。
  • @ZackHinson 是什么让你写了cin &gt;&gt; familytwo[i];这行? familytwo 是一个由 3 个 strings 组成的数组。你想用你从用户那里得到的任何输入覆盖哪一个?

标签: c++


【解决方案1】:

我认为你搞砸了你的 do-while 循环:

i=0;
do {
        cout << "would you like to continue to the sim/y, or would you like to add another family/n" << endl;
        cin >> choice;
        choice = tolower(choice);

         if (choice == 'n')
            cin >> familytwo[i];
         if (choice == 'y')
            cin >> simtype[i];
        i++;
    } while (choice != 'n' && choice != 'y')

编辑:您可以在每一轮中增加 i。

【讨论】:

  • 这正是我要找的!谢谢!但是我会在哪里声明 [i]?
  • 现在好多了..他的逻辑也是错误的,他需要把那些 if else 块放在 do-while..
  • @hacker315 的 anwser 工作,它允许我继续执行程序,但是当输入 Y 或 N 时,它会执行不间断循环,而不是让您输入 familytwo 的信息
  • 如果您输入的不是 'y' 或 'n',它会中断..
  • 我输入了 Y 和 N,它只是重复,不知道为什么@hacker315
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-09
  • 1970-01-01
  • 2019-06-23
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 2012-04-18
相关资源
最近更新 更多