0

I am trying to create a chore chart with a bit of console code but I'm very new to C++ and I'm running into a roadblock trying to figure out how to create a loop that asks the user the following:

  1. How many chore fields they would like to input

  2. Input prompt for each field

  3. When the chosen number of fields have been completed a new prompt should ask if the user would like to input more fields or if they are finished.

  4. If they want to input more, start back at loop for # of fields to input or if finished have it print the chore chart to screen. Very basically as I am still grasping these functions.

Here's my code so far:

#include <iostream>

using namespace std;

int main()
{
    int chores_num;
    while (finished = true) = {
        cout << "How many chores would you like to input?\n"
        cin >> chores_num;

        cout << "Enter Chore\n";
        cin >> chore;
        cout << ""
    }

    return 0;
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Dave
  • 1
  • 1
  • You should go grab a [good book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) :) – Rakete1111 Aug 10 '16 at 02:40
  • That. Go and read a good book on C++. There's nothing wrong with asking a simple question, but mindlessly regurgitating canned code in an answer is unlikely to result in you learning something. Only that the given sequence of characters compiles and works as intended, but without fully understanding how. Learn to invest time in studying and learning one of the most complicated programming languages in the world. There is no instant gratification in C++. – Sam Varshavchik Aug 10 '16 at 02:43
  • I'm working through C++ Programming Language by Stroustrup but as a newbie it's taking some time to gather the concepts. I'm not looking for someone to finish the code, on the contrary I'd be happier if someone could just help with the building blocks that I need to make the concept work. – Dave Aug 10 '16 at 02:44
  • Good book. But for this one sit down and write out what you need to do step by step. Then order the list according to when each item in the list needs to be performed relative to the other items. Then hit the book and figure out how you can use loops to avoid writing code to do the same items over and over. – user4581301 Aug 10 '16 at 02:49

2 Answers2

0

You could think about your problem this way: Get number of chores to input; That number of times, get a specific chore input; Get whether to repeat, and do so if yes.

If you have already covered how to write simple functions, I would suggest breaking up your program into functional (pun intended) parts, each in their own function. This is widely considered good programming practice, and can make programs much more readable. If you aren't comfortable with writing/calling your own functions, don't sweat it for now, but keep this in mind later.

C++ has a bit of a learning curve, but is extremely powerful once you've got the hang of it. Keep it up!

qxz
  • 3,814
  • 1
  • 14
  • 29
0

You need 2 loops, but your code only has 1 loop. You need 1 loop to keep asking if chores should be entered, and 1 loop to enter the actual chores.

For example:

#include <iostream>
#include <string>
#include <limits>

using namespace std;

template <typename T>
T prompt(const char *msg)
{
    T input;
    cout << msg << " ";
    while (!(cin >> input)) {
        cout << "Invalid input, try again: ";
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }

    cin.clear();
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    return input;
}

template <>
string prompt<string>(const char *msg)
{
    string input;
    cout << msg << " ";
    getline(cin, input);
    return input;
}

int main()
{
    int chores_num;
    string chore;
    char choice;

    do {
        chores_num = prompt<int>("How many chores would you like to input?");

        for  (int i = 0; i < chores_num; ++i) {
            chore = prompt<string>("Enter chore:");
            //...
        }

        choice = prompt<char>("Input more chores (Y|N)?");
    }
    while ((choice == 'y') || (choice == 'Y'));

    // print chore chart...

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • This is great! Thank you for the help, it shows me a lot of better thinking in how to reach my goal. However I get a bunch of errors when I try to compile this as is in Code::Blocks. It's main complaint seems to be T not being declared in scope and the placement of ";" on the line cout << msg " "; – Dave Aug 11 '16 at 22:15
  • `T input;` should have been `string input;` in the specialized version of `prompt()`, and `cout << msg " ";` should have been `cout << msg << " ";`. I have corrected these mistakes. – Remy Lebeau Aug 11 '16 at 22:38