I am writing this program for class and it compiles fine but when i debug it it gives me an assertion failure any suggestions would be welcomed i need it to output 20 random sentences.

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
using namespace std;


const char SIZE = 100;

int _tmain(int argc, _TCHAR* argv[])
{
	const char *article[5] = {"the", "a", "one", "some", "any"};
    const char *noun[5] = {"boy", "girl", "dog", "town", "truck"};
    const char *verb[5] = {"drove", "jumped", "ran", "walked", "flew"};
    const char *preposition[5] = {"to", "from", "over", "under", "on"};
    char sentance[40];

int i = 0;

srand((unsigned)time(NULL));
for( i = 0; i <= 20; i++)
{
    strcat_s(sentance, article[rand()%5]);
    strcat_s(sentance,noun[rand()%5]);
    strcat_s(sentance,verb[rand()%5]);
    strcat_s(sentance,preposition[rand()%5]);
    strcat_s(sentance,article[rand()%5]);
    printf("\n\n%s\n\n'", sentance);
}
return 0;
}

Not sure if you need to use arrays and cstrings for your assignment but in C++ there is a string class and vector class that I think would work way better for this.

#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main()
{
	srand(time(NULL)); //seed random
	vector<string> article(5); //make a vector with 5 elements to hold the articles
	article[0] = "the"; article[1] = "a"; article[2] = "one"; article[3] = "some"; article[4] = "any";

	vector<string> noun(5); //make a vector with 5 elements to hold the nouns
	noun[0] = "boy"; noun[1] = "girl"; noun[2] = "dog"; noun[3] = "town"; noun[4] = "truck";

	vector<string> verb(5); //make a vector with 5 elements to hold the verbs
	verb[0] = "drove"; verb[1] = "jumped"; verb[2] = "ran"; verb[3] = "walked"; verb[4] = "flew";

	vector<string> preposition(5); //make a vector with 5 elements to hold the prepositions
	preposition[0] = "to"; preposition[1] = "from"; preposition[2] = "over"; preposition[3] = "under"; preposition[4] = "on";

	vector<string> sentence(20); //make a vector with 20 elements to hold the sentences

	for( unsigned int i = 0; i < sentence.size(); i++ )
	{
		sentence[i] = article[rand()%5] + " " + noun[rand()%5] + " " + verb[rand()%5] + " " + preposition[rand()%5] + " " + noun[rand()%5] + "."; //add all the strings together
		cout << sentence[i] << endl; //output the sentence we just made
	}
	//add pause if needed
	return 0;
}

Storing all the information into the vectors is kind of a pain but they are easy to use.

I don't know about this stdafx business, but from what my compiler tells me you need cstdio to use printf (but you should use std::cout instead). Also, strcat_s is not necessary, just use std::string and the '+' operator. I would even store the words originally in a std::vector<std::string>. I also corrected the spelling of 'sentence' :)

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cctype>
#include <cstdio>
using namespace std;


const char SIZE = 100;

int main(int argc, char* argv[])
{
    const char *article[5] = {"the", "a", "one", "some", "any"};
    const char *noun[5] = {"boy", "girl", "dog", "town", "truck"};
    const char *verb[5] = {"drove", "jumped", "ran", "walked", "flew"};
    const char *preposition[5] = {"to", "from", "over", "under", "on"};

  srand((unsigned)time(NULL));

  std::string sentence;
  for(unsigned int i = 0; i <= 20; i++)
  {
      sentence = std::string(article[rand()%5]) + " " +
                 std::string(noun[rand()%5]) + " " +
                 std::string(verb[rand()%5]) + " " +
                 std::string(preposition[rand()%5]) + " " +
                 std::string(article[rand()%5]);
      std::cout << sentence << std::endl;
  }
  
  return 0;
}

Hope that helps.

David

Haha sfuo you beat me to the punch!

char sentance[40];

int i = 0;

srand((unsigned)time(NULL));
for( i = 0; i <= 20; i++)
{
    strcat_s(sentance, article[rand()%5]);
    strcat_s(sentance,noun[rand()%5]);
    strcat_s(sentance,verb[rand()%5]);
    strcat_s(sentance,preposition[rand()%5]);
    strcat_s(sentance,article[rand()%5]);
    printf("\n\n%s\n\n'", sentance);
}
}

My guess is that you are overrunning your sentence array. First, you should make it larger. 40 characters probably won't consistently be enough. Secondly, you need to clear your sentence variable right after you pring it each time. Using the strcat() function, you are just adding more and more words to the same sentence. You'll run out of your 40 characters rather quickly this way!

Thanks for your input everyone this helps me greatly thank you
I will get the hang of this lol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.