Jump to content
  • Advertisement
Sign in to follow this  
hothead

need some help.

This topic is 5457 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to make a program that randomly prints a word backwards but I can't fix the errors
#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>

using namespace std;

int main()
{
    const int MAX_WORDS = 3;    //make the max number of words three
    const string words[MAX_WORDS] = 
            {{"hello"},    
            {"goodbye"},
            {"guthead"}};
    
    srand(time(0));    //seed the random generator
    int choice = (rand() % MAX_WORDS);
    int theword = words[choice];
    
    string jumble = theword;
    int length = jumble.size();
    for(int i = 0; i < length; i++)
    {
        int var1 = (rand() % length);
        int var2 = (rand() % length);
    
        char temp = jumble[var1];
        jumble[var1] = jumble[var2];
        jumble[var2] = temp;
    }
    
        int k;
        cin>>k;
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement

#include<iostream>
#include<string>
#include<ctime>
#include<cstdlib>

using namespace std;

int main()
{
const int MAX_WORDS = 3; //make the max number of words three
const string words[MAX_WORDS] =
{
"hello",
"goodbye",
"guthead"
};

srand(time(0)); //seed the random generator
int choice = (rand() % MAX_WORDS);
string theword = words[choice];

string jumble = theword;
int length = jumble.size();
for(int i = 0; i < length; i++)
{
int var1 = (rand() % length);
int var2 = (rand() % length);

char temp = jumble[var1];
jumble[var1] = jumble[var2];
jumble[var2] = temp;
}

int k;
cin>>k;
return 0;
}


It should be "string theword" not "int theword"

Share this post


Link to post
Share on other sites
if I remember correctly should this line

const string words[MAX_WORDS] =
{{"hello"},
{"goodbye"},
{"guthead"}};




look like this

const string words[MAX_WORDS] = {"hello", "goodbye", "guthead"};




Doh IFooBar beat me to it

Share this post


Link to post
Share on other sites
what exact code are on those lines?

hmm not sure if you can do this
char temp = jumble[var1];

since jumble is a string and not a char array ... try

char temp = jumble[var1].c_str();

or to make things easier just declare it at the beginning ...
char *jumble = theword.c_str();
char temp = jumble[var1];

and just get the length from theword instead of jumble, so then you can still use the size function ... ie

int length = theword.size();
char *jumble = theword.c_str();

i just think the errors are a result of trying to index the a string type and not a char array.

also instead of using int k; cin >> k; you can just replace with
system ("pause"); so you dont have to declare extra var.

hope this helps is somewhat.


Share this post


Link to post
Share on other sites
Quote:
Original post by ekrax
also instead of using int k; cin >> k; you can just replace with
system ("pause"); so you dont have to declare extra var.

hope this helps is somewhat.


Remember system("pause"); only works on windows ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by ekrax
what exact code are on those lines?

hmm not sure if you can do this
char temp = jumble[var1];

since jumble is a string and not a char array ... try

char temp = jumble[var1].c_str();

or to make things easier just declare it at the beginning ...
char *jumble = theword.c_str();
char temp = jumble[var1];

and just get the length from theword instead of jumble, so then you can still use the size function ... ie

int length = theword.size();
char *jumble = theword.c_str();


i just think the errors are a result of trying to index the a string type and not a char array.

also instead of using int k; cin >> k; you can just replace with
system ("pause"); so you dont have to declare extra var.

hope this helps is somewhat.

it's working fine.[razz]:)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!