473,474 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Random Windows Error

13 New Member
This program is a random sentence generator. I have it running.. more or less. When it complies I will get some correct sentences and I will get some sentences that don't even contain real words. Also each time it compiles I get a Windows error that says that it has to close the program, random when it happens. The program may get 5 sentences in and then get the error or it won't get any and get the error. The code is....

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <ctime>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     char *article[]={"the", "a", "one", "some", "any"};
  11.     char *noun[]={" boy", " girl", " dog", " town", " car"};
  12.     char *verb[]={" drove ", " jumped ", " ran ", " walked ", " skipped "};
  13.     char *preposition[]={"to ", "from ", "over ", "under ", "on "};
  14.     char sen[100];
  15.     int i, r, q, j;
  16.     j=0;
  17.  
  18.     srand(time(NULL));
  19.     while(j<=10)
  20.     {
  21.        for(i=1;i<=7;i++)
  22.        {
  23.        r=(rand()%5+1);
  24.  
  25.        if(i==1)
  26.           strcpy (sen, *(article+r));
  27.        else if(i==2)
  28.           strcat (sen, *(noun+r));
  29.        else if(i==3)
  30.           strcat (sen, *(verb+r));
  31.        else if(i==4)
  32.           strcat (sen, *(preposition+r));
  33.        else if(i==5)
  34.           strcat (sen, *(article+r));
  35.        else if(i==6)
  36.           strcat (sen, *(noun+r));
  37.        else
  38.           strcat (sen, ".");
  39.          }
  40.        q=0;
  41.        while (sen[q]!='\0')
  42.           cout <<sen[q++];
  43.        cout <<endl;
  44.        j++;
  45.     }
  46.  
  47. system("PAUSE");
  48. return 0;
  49. }
  50.  
Any tips would be greatly appreciated. The error and non words are my main concern, but I have a minor secondary question. How would I get the first letter in the sentence to be capital?
Nov 8 '08 #1
4 1701
JosAH
11,448 Recognized Expert MVP
Indexes in C and C++ and Java and C# and a few more languages start at zero,
not at one so simply generate an index value as rand()%5 and skip adding one
to it.

kind regards,

Jos
Nov 8 '08 #2
MonkeyHater
13 New Member
Wow... that fixed it all and made me feel stupid. Thank you so much!
Any ideas on how to change the first letter to a capital, other than having another string with capital letter words to start the copy into the second string?
Nov 8 '08 #3
JosAH
11,448 Recognized Expert MVP
Bookmark this link and search for the 'toupper()' function/macro (it is described in ctype.h or cctype)

kind regards,

Jos
Nov 8 '08 #4
MonkeyHater
13 New Member
Thank you so much! That answered all my questions!
Nov 8 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Robin Becker | last post by:
We've been queried about the randomness of some filenames we're producing. I worked through and seemed to satisfy myself that random is being initialised from the system time in C time(&now)...
6
by: Acacia | last post by:
How would you generate a random number in C++?
0
by: Mehmet Arıdal | last post by:
hi, we developed an erp project using vb .Net and the project running on terminal server, Server 2003 operating system. Some times, our users having the error below, randomly and we don't know why...
16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
5
by: Raterus | last post by:
I'm just throwing this error out for my sanity, I've seen posts about this, but never solutions. I'm using VS.NET 2003, Framework 1.1, and I'm getting a random error about every 1 out of 10 times...
3
by: J_A5 | last post by:
Hi, I recently upgraded my web app from 1.1 to 2.0 and SQL Server 2005. After the upgrade, I am seeing random appdomain restarts. It was not even logging any information untill I turned on logging...
8
by: Nalaka | last post by:
Hi, I get the following error at times... I do not read or write to files. I do query sql server. This error does not happen all the time either..... Have you seen this before.... any help is...
8
by: Daniel | last post by:
Hey guys Using Random(), how random is it, is it possible to be predicted? I have a requirement for a very good random number generator. I was doing something such as: Random randomSeed = new...
17
by: almurph | last post by:
Hi, Hope you can help me with this one. I am trying to create random number between 0 and 1 inclusive of cryptographiuc quality. The problems is though - I don't know how! Here is what I have so...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.