C++ array issue

I know I'm overlooking something trying to build this mad lib simple but my array is starting at the wrong spot or skipping. You can input this code online to test it at https://www.onlinegdb.com/online_c++_compiler if you want to see what I mean.

This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <conio.h>
#include <string>
#include <fstream>

using namespace std;

void Print(string text, ostream &out)
{
	out << text;
}

int main()
{
	string path = "C:\\SoftwareDev\\test.txt";

	int NUM_MADLIB = 12;

	string *madlib = new string[NUM_MADLIB]
	{ "Enter Greeting: ", "Enter Action Verb ending in -ing: ", "Enter Tourist Site: ", "Enter Location: ", 
	"Enter Feeling Ending in -ly: ", "Enter Kind of Souvenir: ", "Enter Weekday: ", "Enter Number under 30: ", 
	"Enter Coworker Name: ", "Enter 4 digit Coworker Phone Number: ", "Enter Adjective: ", "Enter Your Name: " };

	string letter[15] = {" I'm not here, virtually or physically.  I'm off ", " at the ", " in ", ". ", " While I will ", 
	" send you warm thoughts and maybe even a souvenir ", ", ", " I will not reply to your email until ", " or day ", 
		" of next month, (whichever comes first). ", " /n If this is a life-threatening emergency, please call 911.  But if you've gotten this far, you must be ok.  In that case, call ", 
		" at ", ", with any immediate needs. ", " Have a/an ", " day! " };

	for (int i = 0; i < NUM_MADLIB; i++)
	{
		cout << madlib[i] << " \n";
		getline(cin, madlib[i]);
	}

	for (int i = 0; i < NUM_MADLIB; i++)
	{
		Print(letter[i], cout);
		Print(madlib[i], cout);
		if (i == 11)Print(letter[14], cout);
	}

	ofstream fout(path);
	cout << "Would you like to print your story to a text file? (y/n) ";
	char print;
	cin >> print;

	if (print == 'y' || print == 'Y') {
		for (int i = 0; i < NUM_MADLIB; i++)
		{
			Print(letter[i], fout);
			Print(madlib[i], fout);
			if (i == 11)Print(letter[14], fout);
		}
	}

	return 0;
}

Why are you dynamically allocating your array?
You can just do
1
2
const int NUM_MADLIB = 12;
string madlib[NUM_MADLIB] { ... };

But that isn't the actual issue. You're going to have to be more specific about what the actual issue you're having is.
I will say, your "if (i == 11)" checks seem kinda weird (not wrong, just weird). Why not just put that after the array loop itself? By the way, the whole point of using a named constant like "num madlib" is to avoid magic numbers scattered in your code like "11".

"array is starting off at the wrong spot"
What do you mean? Have you gone through your code with a debugger to see how it is starting?

Your madlib number is 12, so you're never printing letter[12], letter[13], if that helps at all.

Of course there's many ways to tackle this, but I might personally do something a bit different... something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
// Example program
#include <iostream>
#include <string>
using std::string;

struct MadlibPhrase {
    string prompt;
    string begin;
    string end;
    string user_input;
};

int main()
{
    const int NumPhrases = 3;
    MadlibPhrase phrases[NumPhrases] = {
        { "Enter a name: ",                "Hello, ",             "!" },
        { "Enter a verb ending in -ing: ", "Do you like ",        "?" }, // inb4 "actually that's a gerund"
        { "Enter an adjective: ",          "Well, I think it's ", "." },     
    };
    
    for (int i = 0; i < NumPhrases; i++)
    {
        std::cout << phrases[i].prompt;
        std::cin >> phrases[i].user_input;
    }
    
    for (int i = 0; i < NumPhrases; i++)
    {
        std::cout << phrases[i].begin << phrases[i].user_input << phrases[i].end << '\n';
    }
}


Enter a name: Hector
Enter a verb ending in -ing: running
Enter an adjective: awful
Hello, Hector!
Do you like running?
Well, I think it's awful.
Last edited on
Topic archived. No new replies allowed.