C++ :: Randomizing Set Of Strings In Structure
Apr 16, 2014
I currently have a structure that contains strings.
What I would like to do is randomize these strings so that for example the user picks an input, one of these strings will display.
Is it possible to do with the rand function or do I have to go about creating my own functing, assigning values to these strings somehow etc. etc.
I tried reading up on it but as far as I could realize you could only use rand () with numbers, set values etc.
Code:
struct sample_a
{
string mot1 = "heej";
string mot2 = "haj";
};
[Code] ....
View 1 Replies
ADVERTISEMENT
Dec 11, 2013
The purpose of doing this is so that the top of the if statements is not preferred over the bottom. I tried assigning enum values to each case. Then choose a random integer r from 0 to the size of the std::list myList containing those enum elements. The enum value is found using it = std::next (myList, r). Then if the if statement corresponding to that enum value is false, then myList.erase (it), and repeat the process with the newly reduce myList. It works, and everything seems nicely randomized. But it is disappointing much slower than when I used the original if-else statements (it is being applied hundreds of times).
Here is a snippet of my code (I decided not to use switch statements because it looked too clumsy):
std::list<FacingDirection> guyFacingDirections = {Positive_x, Negative_x, Positive_y, Negative_y, Positive_xPositive_y, Positive_xNegative_y, Negative_xPositive_y, Negative_xNegative_y};
while (true) {
const int r = rand() % guyFacingDirections.size();
[Code] .....
There is a crowd of girls. Each guy will choose a girl, and then choose a facing direction to dance with his chosen girl. But not all facing directions are possible if someone is standing at the spot he wants to stand at to get his desired facing direction. Without randomizing the if-else statements, most of the guys will end up facing the same direction, which I don't like.
View 18 Replies
View Related
Oct 4, 2014
I just want to make a WORD GUESSING GAME which when you play it and got the correct word will ask you to play again then it will automatically randomized the words in the array list.. then when you play the game, the words will not repeat until you played them all then it will stop.
#include <iostream>
#include <conio.h>
#include <stdio.h>
#include <string>
#include <windows.h>
#include <ctime>
using namespace std;
#include <process.h>
void gotoxy(short x, short y)
[code]....
View 2 Replies
View Related
Jan 4, 2014
I need to take my string of 6 words, then scramble them into a different order, then output to the console. string line1[] = {"I", "Can", "Do", "That", "Hold", "My", "Beer"};
View 1 Replies
View Related
Aug 27, 2013
I am trying to run a programme implementing a function with structures in c... which is:
#include<stdio.h>
#include<conio.h>
struct store {
char name[20];
float price;
int quantity;
[Code] .....
View 1 Replies
View Related
Dec 7, 2014
Why doesn't this compile?
struct hi(){
void other();
}histructure;
void hi::other(){
std::cout << "Hi!" << std::endl;
[Code] ....
Makes no sense the structure is written before the structure member function is called so why is there compile errors ??...
View 3 Replies
View Related
Oct 7, 2014
Is it possible to assign a value to structure member inside the structure.like.....
struct control{
char tbi:2 =0;
char res:1 =0;
};
View 6 Replies
View Related
Mar 17, 2013
how I can create a structure that pointing to another different structure. And also passing structure to function.
View 3 Replies
View Related
Apr 7, 2013
I am programming a translator, and I have it so that it detects words with spaces both in front of and behind them, so I did "string.append(space);" where space equals " ". That added a space to the end, but I still need a space added to the front.
View 1 Replies
View Related
Feb 12, 2014
I have a problem who must print the sentences who have lenght more than 20 characters. I dont know why, but it prints just the first words. Look what i made.
#include<stdio.h>
#include<conio.h>
int main()
[Code]....
For instance :
Give the number of sentences : 3
First sentence : I like the website bytes.com
Second sentence : I like more the website bytes.com
Third sentence : bytes.com
After I compile the program it should print the first two sentences.
View 2 Replies
View Related
May 27, 2013
I'm trying to use a structure in union in the following format:
Code:
union data
{
unsigned char All[10] ;
struct data_pkt
{
unsigned char ack;
unsigned short status;
unsigned short data_length;
unsigned char Data[5];
}format;
}adb; adb.
All has 10 bytes which is equivalent to the structure bytes. ie 6 bytes if unsigned char and 2 short i.e 4 bytes. Thus total 10 bytes is given to adb.All. When I print the struct size I get 12 bytes. This creates problem in obtaining data in union. According to the program:
adb.format.ack should have the address of adb.All[0]
adb.format.status should have the address of adb.All[1]
adb.format.data_length should have the address of adb.All[3]
adb.format.Data[0] should have the address of adb.All[5]
But in actual case this is how memory is allocated:
adb.format.ack assigned to the address of adb.All[0]
adb.format.status assigned to the address of adb.All[2]
adb.format.data_length assigned to the address of adb.All[4]
adb.format.Data[0] assigned to the address of adb.All[6]
Why this is happening? How can I solve this?
View 9 Replies
View Related
Nov 22, 2013
i'd like to ask if it's possible to fill every array in structure to zero. (Without using too much cycles)Something like this (I know that it doesn't work):
Code:
typedef struct {
char name[30], sname[30], adress[30], day, month; int year; char number[9], email[30];
} person;
void create_list(person z[100])
}
[code]....
View 2 Replies
View Related
Nov 28, 2013
Program to create client server interaction for store data into map using structure and pointer(without memory leak).
View 1 Replies
View Related
Aug 14, 2014
I'm trying to create a structure for a buffer that I can use in 2 different code modules. I've defined the structure in the main.h file
typedef struct
{
uint8_t frame[MRFI_MAX_FRAME_SIZE];
uint8_t rxMetrics[MRFI_RX_METRICS_SIZE];
} receiveBuffer_t; //SS; packetBuffer length is 24 Bytes, 20 for data packet, 1 for length, for rxMetrics, 1 for TxCount
Then set it up in main.c
static receiveBuffer_t XDATA receive_buf[8]; //SS; Store up to 16 received packets for debug
This works great in main but I want to also use this structure in another module. In other.c I've included main.h and am trying to use it as follows
extern receiveBuffer_t receive_buf[8]; // Receive buffer
for (i=0; i<PACKET_LENGTH+1; i++)
receive_buf[rec_head_ptr].frame[i] = mrfiIncomingPacket.frame[i];
I get the following error when I compile.
Error[e46]: Undefined external "receive_buf" referred in mrfi ( C:FlowTimerDebugObjmrfi.r51 )
View 3 Replies
View Related
Aug 7, 2013
I have a structure
Code: struct time{
char hours;
char minutes;
char seconds;
char dummy;
};
I have kept dummy as the data to be aligned.I will update hours, minutes, and seconds , but will not use dummy in any case.
If I don't initialize 'dummy' does it make any errors ?
Do I need to initialize hours, minutes, seconds as well before I use the structure ?
If so is there any particular reason ?
View 6 Replies
View Related
Nov 24, 2013
When I tried to run my code I keep getting a "Segmentation fault".
I am trying to write a code that read from a file and put the data into a structure.
The file look like the following:
2001,ABBIGAEL,5
1994,ABBIGAIL,5
1996,ABBIGAIL,8
1997,ABBIGAIL,13
The file have 31 lines.
Code:
#include <stdio.h>
#include <string.h>
int main() {
/* Define a daydata structure */
typedef struct {
[Code]....
View 4 Replies
View Related
Mar 11, 2013
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#define MAX 200
[Code] .....
I made a txt file which contains a necessary information into my project file and tried to read and print it. However, seems like my program is not reading my file at allI named my file as student
View 2 Replies
View Related
Sep 11, 2014
I'm trying to write a simple program that extract the FAT information from a FAT32 virtual Hard Disk.I have the following structures regarding the FAT format:
Code:
//BOOT RECORD
typedef struct NF_BOOT_RECORD
{
unsigned char BS_jumpBoot[3]; //EB 58 90 = JUMP 58 NOP (Jumps to boot code). Also E9 is acceptable.
unsigned char BS_OEMName[8]; //Either MSWIN4.1 or mkdosfs
unsigned short BS_BytesPerSec; //Little endian. The size of a sector. 128,256,512,1024...
unsigned char BS_SecPerClus; //The number of sectors per cluster (1 CLUSTER = BPS*SPC BYTES)
unsigned short BS_RsvdSecCnt; //The boot sectors (this) are included. That makes at least 1.
}
[code]...
Everything seems to work fine. Mostly. The only problem, is that the program gives me the following output:BS_NumFATs shouldn't be 0. In fact, I've checked inside the structure memory, and the information seems correct. BS_NumFATs is 0x02, not 0x00 (It's the byte at offset 0x10, starting at 0x00).
I've checked the order of the structure fields, and their types, comparing them to the FAT specification given by Microsoft (File fatgen103.pdf), and it seems fine, unless I'm missing something. So I don't know why it gives 0 instead of 2, if I'm missing something.It's a Win32 program compiled with GCC version 4.4.0
View 3 Replies
View Related
Jul 23, 2013
Ok, so this assignment is to create a structure that allows input for up to 6 employees that then makes a 6 structure array showing Employee ID, Employee Last Name, Employee Pay Rate, Employee Hours Worked, Employee Pay, and Total Gross Pay for All Employees.
I don't have a printf yet for total gross, but right now I am just trying to tackle the input. Obviously I am not doing it right because although gcc complier is not giving errors the program is not ending when I type 'q' (sentinel issue) or when I reach 6 employees. It just continues input forever. Here is my code so far:
Code:
//Cameron Taylor
#include <stdio.h>
#define MAXARRAY 6
struct Record{
int idnum;
char lname[20];
double pay_rate;
[Code] ........
View 9 Replies
View Related
Apr 9, 2013
I want to get the starting index of structure elements, whoz id are 0,1,2,3 Like in below code col_data[0] (starting id=0) col_data[3] (starting id=1) col_data[5] (starting id=2) col_data[8] (starting id=3) Code:
Code:
typedef struct gg{
int id;
int value;
}
[code]....
How can i skip remaining loop iterations when it get that index and will go back to loop again for getting next element index?
View 7 Replies
View Related
Mar 18, 2014
Code:
#include<stdio.h>
#include<strings.h>
main() {
} struct employ {
char name[50];
float age ;
[Code] .....
View 2 Replies
View Related
Feb 21, 2014
I am reading the book C Programming Language. On Structures, it says:
"The only legal operations on a structure are copying it or assigning to it as a unit, taking its address with &, and accessing its members."
What does it mean assigning a structure as a unit?
View 12 Replies
View Related
Aug 11, 2014
I've used qsort to extract the unique student IDs by jumping every four because there's 80 rows of data and twenty students.
I want to make sure it's all working and there's something very strange going on. If you look at my code, when it comes to initialising the structure with the unique student IDs it will happily print them after each one has been initialised inside the for loop (which suggests to me it's obviously done it) but, however, when I want to print them again outside the for loop in another for loop as a "double-check" it goes horribly wrong, presumably they're not in there at all? If you traverse the code to the bottom you'll see my comments.
Now in my for loop, j is going to be incremented 20 times which will correspond to the #define STUDENTS 20. So imagine the array of structure db[j] , the first loop will set i and j both to zero, retrieve the first unique ID and store it in the structure. Then i is incremented by four to acces the next unique ID and then j is incremented by 1 to insert this new ID in the structure.
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
[Code]....
View 3 Replies
View Related
Oct 9, 2013
how to solve this undeclared error when compiling this code. I assume it has something to do with scope.
C code - 47 lines - codepad
Code:
#include <stdio.h>
int main(void) {
struct bank_account {
[Code].....
View 3 Replies
View Related
Feb 16, 2015
After i set the value in the first structure owners name, i set the cats name equal to it. but when i change the value in the first structure it doesn't change the value in the second structure.
So i need the dogs owners name to be equal to the cats owners name
So if i change the value of the dogs owner it also changes the value of the cats owner.
Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
//stucture
struct structDog {
[Code] ....
View 3 Replies
View Related
Jul 23, 2013
my problem is naming the function larger() with "int". At least that is what my compiler is leading me to believe.
Code:
#include <stdio.h>
struct Date{
int month;
int day;
int year;
};
[code]....
View 8 Replies
View Related