0

Below I am setting up a y/n question in the program, I know I need an identifier named i for the cin line to work in the if's. However, I'm not sure where to put my identifier.

#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";
}
JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • 3
    What's an [I] identifier? Does your program have errors? Does it give the wrong results? – cigien May 25 '20 at 18:51
  • 1
    Fix your indentation. It's rather messy and hard to read right now. You are also missing `#include`s. – Ted Lyngmo May 25 '20 at 18:57
  • 1
    Be a little more precise about your problem? – Umair May 25 '20 at 18:58
  • After the do-while loop, you have braces in the wrong position. if choice is 'n'`, then there's no point checking if choice is 'y'. – cigien May 25 '20 at 18:59
  • The errors im running into are - 1>C:\Users\z16hi\Desktop\CIS 170 Code\Course Project\Course Project.cpp(39,21): error C2065: 'i': undeclared identifier – Zack Hinson May 25 '20 at 19:00
  • Sorry guys im extremely new to this, started two weeks ago. – Zack Hinson May 25 '20 at 19:01
  • Here you use `i` but you haven't declared `i` in the scope you're using it: `cin >> familytwo[i];` – Ted Lyngmo May 25 '20 at 19:02
  • if the anwser is Y i need to program to move onto a "simtype" input. If the anwser is N i need the program to go to familytwo – Zack Hinson May 25 '20 at 19:02
  • @TedLyngmo I'm not sure where to declare i in this particular part of the program – Zack Hinson May 25 '20 at 19:03
  • 1
    @ZackHinson What made you write the line `cin >> familytwo[i];`? `familytwo` is an array of 3 `string`s. Which one do you want to overwrite with whatever input you get from the user? – Ted Lyngmo May 25 '20 at 19:05
  • @TedLyngmo I'm not sure, I'm still completely new to C++ and I'm currently learning to the Y/N portion. – Zack Hinson May 25 '20 at 19:07
  • Hi Zack! When you call `cin >> familytwo[i]`, `cin` will assign what the user inputs into the variable `familytwo[i]`. Here the compiler is telling you "I don't know what is that `i` variable you are using at line 39". When you are receiving the user input, you need to put it in a variable. If you search stack overflow for "Getting user input in C++" you'll find some guidance: https://stackoverflow.com/questions/7944861/getting-user-input-in-c – OLP May 25 '20 at 20:10
  • *"I'm not sure, I'm still completely new to C++"* -- this is not a good excuse. When you write a program, you should: *first* figure out what you want to do, and *second*, figure out how to implement that in code (i.e. *second*, write the code). This excuse suggests you are writing code first. This is a gateway to the dark realm known as [cargo cult programming](https://en.wikipedia.org/wiki/Cargo_cult_programming). Beware that realm! – JaMiT May 25 '20 at 20:52
  • @ZackHinson kindly tick the right answer, if it helped.. – hacker315 May 26 '20 at 11:05

1 Answers1

0

I think you messesd up your do-while loop:

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')

EDIT: you can increment i in every round.

hacker315
  • 1,996
  • 2
  • 13
  • 23