473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Limiting possibilities with string arrays

Hello again! I am still having problems with the code under the title "newbie woes", but I have moved on for now to another exercise. The book has asked me to write a program that will use two string arrays to select a toy out of a bag, a ball or cube in 1 of four colours. There should also only be one ball and cube for each colour, which is that part I am having problems with. Also, I am not very clear on how this code works, I will comment it and if you could let me know if I am on the right track, that would be great! So here is my code so far:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream> // Is this just to allow the basic C++ code?
  2. #include <stdlib.h> // Not sure what this header does
  3. #include <time.h>  // I think this allows me to use the computer's clock 
  4. #include <math.h> // I think this allows me to use mathematical symbols such as *, /, ==, +, - etc... 
  5. using namespace std; // Not sure what this does, I just put it in every program like a good little boy...
  6.  
  7. int rand_0toN1(int n); // declares function "rand_0toN1" multiplied by integer "n"
  8. void toy_ball_cube(); // dec;ares function "toy_ball_cube" that does not return a value
  9.  
  10. char *colour[4] = {"red", "blue", "orange", "green"}; // A string array for the four colours
  11. char *shape[2] = {"ball", "cube"}; // A string array for the two shapes
  12.  
  13. int main() {  // Begins obligatory "main" function
  14.     int n, i; // declares integers n and i
  15.  
  16.     srand(time(NULL));  // Does this create a random seed from the clock time 
  17.                         // multiplied by a null value? Could I use "1" in place of "NULL"? 
  18.  
  19.     while (1) {  //  I understand the concept of a while loop, but the 1 I don't get
  20.           cout << endl;
  21.           cout << "Enter no. of toys to take out (enter 0 to exit): ";
  22.           cin >> n;
  23.           if (n == 0)  
  24.              break;
  25.           for (i = 1; i <= n; i++) // for loops confuse me
  26.              toy_ball_cube();
  27.           }
  28.     return 0;
  29.     }
  30.  
  31.  
  32. /* The last two functions I don't understand, I just know that the code works */
  33. void toy_ball_cube() {
  34.      int c;
  35.      int s;
  36.  
  37.      c = rand_0toN1(4); 
  38.      s = rand_0toN1(2);
  39.      cout << "It is a " << colour[c] << " " << shape[s] << endl;
  40.      }
  41.  
  42. int rand_0toN1(int n) { 
  43.     return rand() % n;
  44. }
  45.  
Jan 28 '07 #1
0 1130

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

Similar topics

3
by: Java script Dude | last post by:
Some programmers prefer to stay with native level data structures such as string arrays instead of using Object based data structures such as ArrayList. From and efficiency point of view. Are...
4
by: Adam | last post by:
Greetings, I am creating a web application, which I will be selling licenses to be able to use it. So Customer Group A, could purchase 3 licenses, Customer Group B could purchase 30. With...
16
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
17
by: Chad Myers | last post by:
I've been perf testing an application of mine and I've noticed that there are a lot (and I mean A LOT -- megabytes and megabytes of 'em) System.String instances being created. I've done some...
1
by: Paul | last post by:
Hi just wondering how to limit the string size for a data grid column, can this be done from the properties box, data formatting expression? If so what would be the expression to limit it to 20...
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
18
by: greenflame | last post by:
I want to make a function that does the following. I will call it thefunc for short. '||Char>>' I tried the following def thefunc(s): s = "||" + s + ">>"
3
by: SneakyElf | last post by:
i am very green with c++ so i get stuck on very simple things anyway, i need to write a program that would read data from file (containing names of tv shows and their networks) one line at a time...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.