Unscramble words program- output image and score not as parameter

I've been trying for 2 days but I can't think of an answer. If you can spot the problems in my code, i'd appreciate it loads. I'm trying to a code that tells you to unscramble words, starting from a 3 letter word to a 12 letter word.

I tried doing this using Dev c++. Lines 44-105 cause infinite looping. I can see why but I can't seem to get a solution to repeat the menu selection 1-3. And lines 102-105 are a pain in the butt. It appears every single time after a y or n. Lines 77-84. Even lines 85-94 don't give out the right output.

The problem with my code is that it doesn't add up the scores. Can you teach me how to do that? I need to do it without a global variable. Declaring it in main() and passing it on to the other functions as parameters.

Another question. How can i output a picture? That's supposed to be my output when the user enters the number 2. How do i create another window to display the object?

Functions SecondWord to TenthWord are about the same.
This is my code:

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
97
98
99
100
101
102
103
104
105
106
107
108
#include <cstdlib>
#include <iostream>
#include <string>
#include <cctype>

using namespace std;

void FirstWord(char First[4], int mark)//don't know how to get marks to add up
{
     char str[4];
     int result, i;
     cout<<"1 - ADL: ";
     cin>>First;
     for(i=0; First[i]!='\0'; i++)
     {
                    str[i]=toupper(First[i]);
     }
     result=strcmp(str,"LAD");
     if(result==0)
     {
                  mark=mark+10;
                  cout<<"Well Done! Your Marks Is: "<<mark;
                  cout<<endl<<endl<<endl;    
     }
     else
     {
         cout<<"Sorry! You Are Wrong! Your Marks Is: "<<mark;
         cout<<endl<<endl<<endl;              
     }
}

int main(int argc, char *argv[])
{
    char Str[12];
    int choice, again, Score=0;
    system("color F2");
    cout<<"Welcome To Computer Scrabble!"<<endl<<endl<<endl;
    cout<<"Please Choose The Following Options:"<<endl;
    cout<<"1.Play Scrabble."<<endl;
    cout<<"2.Developers Picture."<<endl;
    cout<<"3.Quit."<<endl<<endl<<endl;
    cin>>choice;
    cout<<endl<<endl<<endl;
    while(choice==1||choice==2||choice==3)
    {
                                          if(choice==1)
                                          {
                                                       cout<<"Please Re-arrange The Following Words: "<<endl<<endl<<endl;
                                                       FirstWord(Str, Score=0);
                                                       TenthWord(Str, Score);
                                                       cout<<Score<<endl<<endl;
                                                       cout<<"Would You Like To Try The Program Again? (y/n): ";
                                                       cout<<endl<<endl<<endl;
                                                       cin>>again;
                                                       cout<<endl<<endl<<endl;
                                                       if(again=='y'||'Y')
                                                       {                              
                                                                                      choice=0;
                                                                                      cout<<"1.Play Scrabble."<<endl;
                                                                                      cout<<"2.Developers Picture."<<endl;
                                                                                      cout<<"3.Quit."<<endl<<endl<<endl;
                                                                                      cin>>choice;
                                                       }
                                                       else
                                                       {
                                                                                      exit(0);
                                                       }                   
                                          }
                                          if(choice==2)
                                          {
                                                       cout<<"Smexy ppl"<<endl<<endl;
                                                       cout<<endl<<endl<<endl;
                                                       cout<<"Would You Like To Try The Program Again? (y/n): ";
                                                       cout<<endl<<endl<<endl;
                                                       cin>>again;
                                                       cout<<endl<<endl<<endl;
                                                       if(again=='y'||'Y')
                                                       {                              
                                                                                      choice=0;
                                                                                      cout<<"1.Play Scrabble."<<endl;
                                                                                      cout<<"2.Developers Picture."<<endl;
                                                                                      cout<<"3.Quit."<<endl<<endl<<endl;
                                                                                      cin>>choice;
                                                       }
                                                       else
                                                       {
                                                           if(again=='n'||'N')
                                                           {
                                                                                      exit(0);
                                                           }    
                                                           else
                                                           {
                                                               cout<<"Please Enter y or n Only!"<<endl<<endl<<endl; 
                                                           }
                                                       }
                                          }
                                          if(choice==3)
                                          {
                                                       exit(0);
                                          }                                      
    } 
    if(choice!=1&&choice!=2&&choice!=3)
    {
                                       cout<<"Please Enter Menu Numbers 1 to 3 Only!"<<endl<<endl<<endl;
    }                       
    system("PAUSE");
    return EXIT_SUCCESS;
}


At a loss...
Last edited on
I can only help you out with the logic in the program because I am not familiar with dev c++ and I'm just a fledgling programmer. hopefully this helps you along a bit though:


after line 43, instead of creating a loop that goes through the choices try this one. if the choice is not 1,2, or 3 it displays an error message and asks the user to enter their choice again. this should fix lines 102-105 by error trapping the input from the user before you go through the choices.

while(choice!=1&&choice!=2&&choice!=3)
{
cout << "Please enter numbers 1 - 3 only! << endl << endl << endl;
cout<<"Please Choose The Following Options:"<<endl;
cout<<"1.Play Scrabble."<<endl;
cout<<"2.Developers Picture."<<endl;
cout<<"3.Quit."<<endl<<endl<<endl;
cin>>choice;
}

now that you know you have a choice that is 1 to 3 create a loop for choices 1 and 2(if the user entered 3 it will skip the loop and go straight to the end of the program.) it should be something like this:

while(choice == 1 || choice == 2)
{
if (choice == 1)
{
do //created a nested post-test loop to ask the user if the want to play the game again
{
char again = ''; //creates the variable char inside the loop to make it more efficient. this is the only place you //need it.

cout << //scrabble game code goes here.

//prompt the user to play again inside the loop
cout << "Would you like to play again?" << endl;
cin >> again
}while(again == 'Y' || again == 'y')

if(choice == 2)
{
//display developer picture code
}
//now display the menu and prompt the user for a decision
cout << "What would you like to do?" << endl << endl;
cout<<"Please Choose The Following Options:"<<endl;
cout<<"1.Play Scrabble."<<endl;
cout<<"2.Developers Picture."<<endl;
cout<<"3.Quit."<<endl;
cin >> choice;
cout<<endl<<endl;

//error trap the user's choice again by using the same loop as before

while(choice!=1&&choice!=2&&choice!=3)
{
cout << "Please enter numbers 1 - 3 only! << endl << endl << endl;
cout<<"Please Choose The Following Options:"<<endl;
cout<<"1.Play Scrabble."<<endl;
cout<<"2.Developers Picture."<<endl;
cout<<"3.Quit."<<endl;
cin >> choice;
cout<<endl<<endl;
}
}//end of while loop for choices

system("PAUSE");
return EXIT_SUCCESS;
}//end of main function

hope that helps a bit.

idk about outputting a picture or adding your marks up though. good luck. post the solution if you get it solved :)
Hi chriswsy,

My first point is to put the code inside the braces after if(choice==1) into a function which is called straight after if(choice==1). Actually you would be better to use a switch statement instead of those if statements.

1
2
3
4
5
6
7
switch (choice)
   case 1:
     PlayScrabble();
   case 2:
     DevelopPicture();
   case 3:
     exit(0);


Now declare the functions before main() and put their implementation after main().

Functions SecondWord to TenthWord are about the same.


If the functions are the same, why not have one function called ProcessWord? You might need to put your 10 words into an array.


You really do want avoid writing the same code 10 times over.

This code is repaeted 3 times
1
2
3
4
5
choice=0;
                                                                                      cout<<"1.Play Scrabble."<<endl;
                                                                                      cout<<"2.Developers Picture."<<endl;
                                                                                      cout<<"3.Quit."<<endl<<endl<<endl;
                                                                                      cin>>choice;


Using the switch statement and calling functions will help avoiding this also.

Outputting a picture is beyond the scope of a console app like this, to do that you need a GUI app, which is whole different thing.

To add up marks find out about declaring the variable mark as static.

hope this helps

TheIdeasMan

Topic archived. No new replies allowed.