debug assertion failed

Trying to print 10 random sentences. Sometimes it works and sometimes i get this

debug assertion failed
expression (L"buffer is too small"&&0)

ALSO can't figure out how to capitalize first letter of first word.
THANK YOU

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
  Put the code you need help with here.
#include<iostream>
#include<cstring>
#include<ctime>
#include<cmath>
#include<cctype>
using namespace std;

void main()
{
	char article[5][5] = { "the", "a", "one", "some", "any" };
	char noun[5][5] = { "boy", "girl", "dog", "town", "car" };
	char verb[5][8] = { "drove", "jumped", "ran", "walked", "skipped" };
	char preposition[5][6] = { "to", "from", "over", "under", "on" };
	char sent[36];
	int i;
	srand(time(NULL));
	for (i = 1; 1 < 10; i++)
	{

		strcpy_s(sent, article[rand() % 5 + 1]);
		strcat_s(sent, " ");
		strcat_s(sent, noun[rand() % 5 + 1]);
		strcat_s(sent, " ");
		strcat_s(sent, verb[rand() % 5 + 1]);
		strcat_s(sent, " ");
		strcat_s(sent, preposition[rand() % 5 + 1]);
		strcat_s(sent, " ");
		strcat_s(sent, article[rand() % 5 + 1]);
		strcat_s(sent, " ");
		strcat_s(sent, noun[rand() % 5 + 1]);
		strcat_s(sent, ".");



		cout << sent << endl;
	}
arrays are 0-indexed, you should not +1 to the remainder.

Also nobody creates strings like that in C, very unpractical.

You instead use char* strings[4] = {"1","2","3","4"};, another improvement is to not use strcpy at all and directly print into cout, and even better use strings instead.

Also you can use toupper to make a letter capitalized, but you would probably need to create a temporary copy of the string and modify it.

Also void main is wrong, it should always be int main.

And I didn't notice the 1 < 10, you should really check the warnings your compiler gives because it will ALWAYS catch that.
Last edited on
Your for loop is wrong. 1 < 10 will always be true.
main should return an int. Your buffer is too small.
Since you use streams you should also get rid of this old C crap.
Here's how to do it in C++11
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
#include <iostream>
#include <array>
#include <string>
#include <ctime>
#include <cctype>

using namespace std;

const int SIZE = 5;

int main()
{
  array<string, SIZE> article = { "the", "a", "one", "some", "any" };
  array<string, SIZE> noun = { "boy", "girl", "dog", "town", "car" };
  array<string, SIZE> verb = { "drove", "jumped", "ran", "walked", "skipped" };
  array<string, SIZE> preposition = { "to", "from", "over", "under", "on" };
  string sent;
  sent.reserve(128);

  srand(time(nullptr));
  for (int i = 1; i < 10; i++)
  {
    sent.clear();
    sent += article[rand() % SIZE];
    sent += " ";
    sent += noun[rand() % SIZE];
    sent += " ";
    sent += verb[rand() % SIZE];
    sent += " ";
    sent += preposition[rand() % SIZE];
    sent += " ";
    sent += article[rand() % SIZE];
    sent += " ";
    sent += noun[rand() % SIZE];
    sent += ".\n"; 
    sent[0] = toupper(sent[0]);
    cout << sent << endl;
  }

}

Output:

A car jumped over one town.

Some boy drove to some girl.

One boy skipped from one girl.

Some car drove on some town.

One town jumped over one girl.

One town skipped from some boy.

Any car drove to any dog.

One boy walked over the girl.

Any girl jumped under a car.
Topic archived. No new replies allowed.