Correct?


#include <string>
#include <cstdlib>
#include <ctime>
#include <stdio.h>




int main()

{
int score,choice,length;
string theword, thehint,jumble;
score = 0;

enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 12;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"door", "Pasukan ito!"},
{"mouse", "Small and eats cheese."},
{"candle", "ginagamit pag brownout"},
{"cocoa", "Uses to make chocolate."},
{"television", "What you watch your shows on."},
{"paul", "my name"},
{"paper","madalas hinihingi ng estudyante"},
{"wall", "Do you feel like banging your head against something?"},
{"glasses", "These might help you see the answer" },
{"friend", "kami ito. pede ba yon? " },
{"computer", "naadik na mga kabataan dito"},
{"jumble", "It's what the game is all about!",}
};

srand(time(0));

choice = (rand() % NUM_WORDS);
theword = WORDS[choice][WORD];
thehint = WORDS[choice][HINT];




jumble = theword;
length = jumble.size();
for (int i = 0; i < length; ++i)
{
int index1 = (rand() % length);
int index2 = (rand() % length);
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
score = length;


}



printf ("\t\t\t**************************\n");
printf ("\t\t\t* JUMBLED WORD PROGRAM *\n");
printf ("\t\t\t**************************\n");
printf("\n");
printf("\n");
printf( " -------------------------------\n");
printf( "| Enter 'hint' for a hint. |\n");
printf( "| Enter 'quit' to quit the game.|\n");
printf( " -------------------------------\n\n");
printf( "SIMULAN NA ANG LARO!!!\n\n\n\n");
printf("The jumble is %s", jumble); // cout<< "The jumble is" << jumble;
string guess;
printf("\n\nYour guess: ");
scanf("%s", guess); // cin>>guess;



while ((guess != theword) && ( guess != "quit"))
{

if (guess == "hint")
printf("%s",thehint); // cout<<thehint



else
printf("Sorry that is not it...");
--score;

printf("\n\nYour guess: ");
printf("%s", guess); // cin>> guess;

}

if (guess == theword)
printf("\nThat's it! You guessed it! You have a score of %d", score);

printf("\nThanks for playing!\n");


system("pause");

return 0;

}



please help me. I converted this to printf scanf but suddenly appeared this error... cannot pass objects of non-trivially-copyable type 'struct std::string' through '...'
Last edited on
printf and scanf (especially as C functions) have no concept of the std::string class. They accept pointers to C-strings(char*), for example:
1
2
3
4
5
6
7
8
9
10
#include <stdio.h>

int main(){
   char buffer[80];
   printf("Please enter a word: ");
   scanf("%s", buffer);
   printf("You entered %s", buffer);

   return 0;
}


Edit:
Typos.
Last edited on
still got an error. :( please help me. :( i've pm you.. please help me
Use char* or char[] instead of string (you know, changing the types of your theword, thehint, jumble, etc. variables[/code].

http://www.cprogramming.com/tutorial/c/lesson9.html
The obvious fix would be to use std::cout for output, but if you must use C functions, you should feed them C-strings:

printf("The jumble is %s", jumble.c_str());
Last edited on
cire +1

Prefer std::string for variable length strings.
And the type-safe C++ input output library over C-style i/o.

Yes, the alternative indicated by comments in your code
1
2
3
// cout<< "The jumble is" << jumble;
// cin>>guess;
// etc.  
is the way to go.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <string>
#include <cstdlib>
#include <ctime>
//#include <stdio.h>
#include <iostream>

int main()
{
    using namespace std ;

    int score,choice,length;
    string theword, thehint,jumble;
    score = 0;

    enum fields {WORD, HINT, NUM_FIELDS};
    const int NUM_WORDS = 12;
    const string WORDS[NUM_WORDS][NUM_FIELDS] =
    {
        {"door", "Pasukan ito!"},
        {"mouse", "Small and eats cheese."},
        {"candle", "ginagamit pag brownout"},
        {"cocoa", "Uses to make chocolate."},
        {"television", "What you watch your shows on."},
        {"paul", "my name"},
        {"paper","madalas hinihingi ng estudyante"},
        {"wall", "Do you feel like banging your head against something?"},
        {"glasses", "These might help you see the answer" },
        {"friend", "kami ito. pede ba yon? " },
        {"computer", "naadik na mga kabataan dito"},
        {"jumble", "It's what the game is all about!",}
    };

    srand(time(0));

    choice = (rand() % NUM_WORDS);
    theword = WORDS[choice][WORD];
    thehint = WORDS[choice][HINT];




    jumble = theword;
    length = jumble.size();
    for (int i = 0; i < length; ++i)
    {
        int index1 = (rand() % length);
        int index2 = (rand() % length);
        char temp = jumble[index1];
        jumble[index1] = jumble[index2];
        jumble[index2] = temp;
        score = length;


    }



    cout << "\t\t\t**************************\n" ;
    cout << "\t\t\t* JUMBLED WORD PROGRAM *\n" ;
    cout << "\t\t\t**************************\n" ;
    cout << "\n" ;
    cout << "\n" ;
    cout <<  " -------------------------------\n" ;
    cout <<  "| Enter 'hint' for a hint. |\n" ;
    cout <<  "| Enter 'quit' to quit the game.|\n" ;
    cout <<  " -------------------------------\n\n" ;
    cout <<  "SIMULAN NA ANG LARO!!!\n\n\n\n" ;
    cout << "The jumble is " << jumble ; // cout<< "The jumble is" << jumble;
    string guess;
    cout << "\n\nYour guess: " ;
    cin >> guess;



    while ((guess != theword) && ( guess != "quit"))
    {

        if (guess == "hint")
            cout << thehint; // cout<<thehint



        else
            cout << "Sorry that is not it..." ;
        --score;

        cout << "\n\nYour guess: " ;
        cin >> guess ; // cin>> guess;

    }

    if (guess == theword)
        cout << "\nThat's it! You guessed it! You have a score of " << score << '\n' ;

    cout << "\nThanks for playing!\n" ;
}
sir JLBorges,
i mean, i need to convert this to printf scanf in dev c++.. i'm sorry.
Oh, that's right, stirng::c_str() returns a pointer to the internal string, so calling scanf with the pointer would modify the original object, too?

Also, the OP stated that the program was being converted to use C I/O.... I'm sure there must be a reason for that.
sir cire, how about this sir?


printf("\n\nYour guess: ");
scanf("%s", guess); // cin>>guess;   // this the error.



while ((guess != theword) && ( guess != "quit"))
{

if (guess == "hint")
printf("%s",thehint); // cout<<thehint     // this the error.



else
printf("Sorry that is not it...");
--score; 

printf("\n\nYour guess: ");
printf("%s", guess); // cin>> guess;     // this the error.

}
Oh, that's right, stirng::c_str() returns a pointer to the internal string, so calling scanf with the pointer would modify the original object, too?


c_str returns a pointer to const char, so feeding it to scanf would not be appropriate. I would very strongly suggest using std::cin for input.
If you must:

1
2
3
4
5
6
7
string guess;
printf("\n\nYour guess: ");
//cin >> guess;
guess.resize(100) ; // make space to hold upto 99 characters
// if( scanf( "%99s", &guess.front() ) == 1 ) // oops! C++11
if( scanf( "%99s", &( guess[0] ) ) == 1 ) // try to read max 99 characters + null char
    guess.resize( guess.find( char(0) ) ) ; // truncate the string to actual numbrer of chars read 
Last edited on
sire cire, please help me to change this to scanf :(

printf("\n\nYour guess: ");
scanf("%s", guess); // cin>>guess;   
sir JLBorges, i get an error..

[Error] 'std::string' has no member named 'front'



  string guess;
    printf("\n\nYour guess: ");
    guess.resize(100) ;  //cin>> guess;
    if( scanf( "%99s", &guess.front() ) == 1 ) 
    guess.resize( guess.find( char(0) ) ) ;

Yeah, I had overlooked that 'front' was added by C++11.

I've edited my earlier post with an equivalent alternative construct that should work with older compilers.
Sir JLBorges,
I'm Using Dev C++.. i need to fix this error.


printf("\n\nYour guess: ");
scanf("%s", guess); // cin>>guess;   // this is the error.



while ((guess != theword) && ( guess != "quit"))
{

if (guess == "hint")
printf("%s",thehint.c_str()); // cout<<thehint     



else
printf("Sorry that is not it...");
--score; 

printf("\n\nYour guess: ");
scanf("%s", guess); // cin>> guess;     // this is the error

}
Repeat:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
printf("\n\nYour guess: ");
//scanf("%s", guess); // cin>>guess;   // this is the error.
guess.resize(100) ; // make space to hold upto 99 characters
if( scanf( "%99s", &( guess[0] ) ) == 1 ) // try to read max 99 characters + null char
    guess.resize( guess.find( char(0) ) ) ; // truncate the string to actual numbrer of chars read


while ((guess != theword) && ( guess != "quit"))
{

if (guess == "hint")
printf("%s",thehint.c_str()); // cout<<thehint     



else
printf("Sorry that is not it...");
--score; 

printf("\n\nYour guess: ");
//scanf("%s", guess); // cin>> guess;     // this is the error
guess.resize(100) ; // make space to hold upto 99 characters
if( scanf( "%99s", &( guess[0] ) ) == 1 ) // try to read max 99 characters + null char
    guess.resize( guess.find( char(0) ) ) ; // truncate the string to actual numbrer of chars read

}



> I'm Using Dev C++

Switch to a current version of Dev-C++ - for instance http://sourceforge.net/projects/orwelldevcpp/



An alternative to:
1
2
3
4
//scanf("%s", guess); // cin>> guess;     // this is the error
guess.resize(100) ; // make space to hold upto 99 characters 
if( scanf( "%99s", &( guess[0] ) ) == 1 ) // try to read max 99 characters + null char
    guess.resize( guess.find( char(0) ) ) ; // truncate the string to actual numbrer of chars read 


is
1
2
3
4
5
6
//scanf("%s", guess); // cin>> guess;     // this is the error
{
    char temp[100] = {0} ; // make space to hold upto 99 characters
    scanf( "%99s", temp ) ;  // try to read max 99 characters + null char
    guess = temp ; // assign what was read to 'guess'
}
Last edited on
Topic archived. No new replies allowed.