Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,119 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,996 people online right now. Registration is fast and FREE... Join Now!




Flames game. need help please

 
Reply to this topicStart new topic

Flames game. need help please, need help in making this work

lex_12
post 11 May, 2008 - 05:58 AM
Post #1


New D.I.C Head

*
Joined: 7 May, 2008
Posts: 5

CODE


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


main(){

    char str1[30];
    char str2[30];
    int x,y,m,n,i;

    printf("Enter a name: \n");
    gets(str1);


    printf("Enter another name: \n");
    gets(str2);
        
        
        
        for(x=0;x>-1;x++){
            for(y=strlen(str1);y>-1;y--)
            if(strcmp(str1[x],str1[y]!=0))
                continue;
            
            else
            
            m=strlen(str1[x]+str1[y]); //i need to add them if they have the same char
                
            }return 1;
        

                //i think this is for checking the second string if they have the same characters    
        for(x=0;x>-1;x++){
            for(y=strlen(str2);y>-1;y--)
            if(strcmp(str1[x],str1[y]!=0))
                continue;
            
            else
            
            n=strlen(str1[x]+str2[y]);
                
            }return 1;
                
            
            i = m+n%6;
        


    switch(i){
            case 0:printf("Sweethearts\n");
        break;
            case 1:printf("Lovers\n");
        break;
            case 2:printf("Anger\n");
        break;
            case 3:printf("Marriage\n");
        break;
            case 4:printf("Enemies\n");
        break;
            case 5:printf("Friends\n");
        break;
        }
  
    
}

User is offlineProfile CardPM

Go to the top of the page

Ambercroft
post 11 May, 2008 - 02:10 PM
Post #2


D.I.C Head

Group Icon
Joined: 5 Jan, 2007
Posts: 105



Thanked 3 times

Dream Kudos: 25
My Contributions


What you should do is try to compile the code. You will find many errors at this point. Fix them then you will find that it won't run because of logical errors.

Then you can ask how to fix the code to make it do what you want. I can fix it but it definitely won't do what you want it to do. cool.gif


User is offlineProfile CardPM

Go to the top of the page

lex_12
post 12 May, 2008 - 05:01 AM
Post #3


New D.I.C Head

*
Joined: 7 May, 2008
Posts: 5

ok.. thnx ambercroft.. i modified my code a bit because as you said there were alot of logical errors.. i simplified it to this.. but i think my algorithm is bit off.

Can anyone help me with an algorithm that will compare str1 and str2 and then count the number of the same characters they have and modulo it by 6. Any help or idea will be very much appreciated.


CODE

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


main(){

    char str1[30];
    char str2[30];
    int x,y,m,i;

    printf("Enter a name: \n");
    gets(str1);


    printf("Enter another name: \n");
    gets(str2);
        
        
        
        for(x=0;strlen(str1)<x);x++){
            for(y=strlen(str2);y>-1;y--){
            if(strcmp(str1[x],str2[y]==0))
            
            
                m=m+1;
                break;
            }
    }
            
            
                
        
            i = m+n%6;
        


    switch(i){
            case 0:printf("Sweethearts\n");
        break;
            case 1:printf("Friends\n");
        break;
            case 2:printf("Lovers\n");
        break;
            case 3:printf("Anger\n");
        break;
            case 4:printf("Marriage\n");
        break;
            case 5:printf("Enemy\n");
        break;
        }
  
    
}
        
User is offlineProfile CardPM

Go to the top of the page

Ambercroft
post 12 May, 2008 - 06:37 PM
Post #4


D.I.C Head

Group Icon
Joined: 5 Jan, 2007
Posts: 105



Thanked 3 times

Dream Kudos: 25
My Contributions


Ok, If your code is clear it will most likely help you find your way. Any code I help on gets formatted to my liking which helps me understand and correct problems.

Take your calcuation for string length out of the loop. It won't matter for code speed here but build good habits.

strl1 = strlen(str1);
strl2 = strlen(str2);

Your comparison is mixed. x will always be larger. Try 'x < strl1', in English 'Do the code in the brackets because this comparison of x < strl1 is still true'.

Do the same in the other loop.

Get rid of 'n',break(only in the for loop) declare all variables.

Look up 'gets' and find out why you should replace it with 'fgets'.

Try all this and post the working code smile.gif

This post has been edited by Ambercroft: 12 May, 2008 - 06:43 PM
User is offlineProfile CardPM

Go to the top of the page

christian0159
post 28 Sep, 2008 - 05:24 AM
Post #5


New D.I.C Head

*
Joined: 28 Sep, 2008
Posts: 2

i cant get it i got a problem can convert int to constant character
here what i write

CODE

#include<stdio.h>
#include <conio.h>
#include <string.h>

int main()
{
char str1[30], str2[30];
int x,y,strl1,strl2,i,n;
int m=0;
clrscr();
printf("name for boy\n");
gets(str1);
printf("\nname for girl\n");
gets(str2);
strl1=strlen(str1);
strl2=strlen(str2);

               for(x=0 x<strl1; x++){
                   for(y=0 y<strl2; y++){
                   if(strcmp(str1[x],str2[y])==0)
                  
                   m=m+1;
                    }
        }
n=m/6;
i=m-n;

switch(i) {
           case 0:printf("Sweetheart\n");
           break;
           case 1:printf("Friends\n");
           break;
           case 2:printf("Lovers\n");
           break;
           case 3:printf("Anger\n");
           break;
                   case 4:printf("Marriage\n");
           break;
           case 5:printf("Enemy\n");
           break;
           }
getch();
}


This post has been edited by christian0159: 28 Sep, 2008 - 05:41 AM
User is offlineProfile CardPM

Go to the top of the page

JackOfAllTrades
post 28 Sep, 2008 - 07:52 AM
Post #6


D.I.C Addict

Group Icon
Joined: 23 Aug, 2008
Posts: 500



Thanked 44 times

Dream Kudos: 25
My Contributions


cpp
if(strcmp(str1[x],str2[y])==0)


str1[x] and str2[y] aren't strings, they're individual characters in their respective character arrays, so you should just compare them for equality:

cpp
if (str1[x] == str2[y]) 
++m;
User is offlineProfile CardPM

Go to the top of the page

christian0159
post 29 Sep, 2008 - 12:35 AM
Post #7


New D.I.C Head

*
Joined: 28 Sep, 2008
Posts: 2

thx for the help but there is still problem on it

here is my code

CODE

#include <stdio.h>
#include <conio.h>
#include<string.h>

void main()
{
char str1[30], str2[30];
int x,y,i,strl1,strl2;
int m=0;
clrscr();
printf("boy name\n");
gets(str1)
printf("girl name\n");
gets(str2);
strl1=strlen(str1);
strl2=strlen(str2);

for(x=0; x<strl1; x++){
for(y=0; y<strl2; y++){
if(str1[x]==str2[y])
++m;
}
}
i=m%

if(i==0 && m==0){
printf("No letters match FLAMES can be used");
{
if(i==0 && m!=0){
printf("Sweethearts");
{
if(i==1){
printf("Friends");
}
if(i==2){
printf("Lovers");
}
if(i==3){
printf("Anger");
}
if(i==4){
printf("Marriage");
}
if(i==5){
printf("Enemy");
}
getch();
}



I try to compare "aas" to "adf" it conclusion said Lovers but counting them
it should be 3 so Anger because the game add all the same letters and add their total so it count aaa so 3 if i try "aaggs" to "agagthh" total should be 8
m=8 i=m%6 so its 3 Anger also

This post has been edited by christian0159: 29 Sep, 2008 - 02:56 AM
User is offlineProfile CardPM

Go to the top of the page

ungazzz
post 4 Oct, 2008 - 10:57 PM
Post #8


New D.I.C Head

*
Joined: 4 Oct, 2008
Posts: 1

Here try this:



CODE

/*code ni ungazzz*/
#include <stdio.h>
#include <string.h>

char name1[100],name2[100],name11[100],name12[100];
int x,y,z,a;
int num,rep,rep2;
int len1,len2;

main() {
clrscr();
rep=0;
printf("Input the first name: ");
gets(name1);
strupr(name1);
printf("Input the 2nd name: ");
gets(name2);
strupr(name2);
y=0;
/* for checking of names */
len1=strlen(name1);
len2=strlen(name2);
rep=0;
for(x=0;x<len1;x++){
  if(name1[x]=='.') {
    name1[x]=' ';}
  if(name1[x]=='/') {
    name1[x]=' ';}
  for(y=0;y<len2;y++){
     if((name1[x]==name2[y])&&(name2[y]!=' ')&&(name1[x]!=' ')) {
    num++;
       for(z=0;z<len1;z++) {
          if((name1[x]==name1[z])&&(x!=z)&&(name1[x]!=' ')&&(name1[z]!=' ')) {
          num++;
          name1[z]=' ';
          }
       }/*end for z*/
       for(a=0;a<len2;a++) {
          if((name1[x]==name2[a])&&(name1[x]!=' ')&&(name2[a]!=' ')&&(rep==0)) {
          num++;
          rep++;
          name2[a]=' ';
          }
          if((name1[x]==name2[a])&&(name1[x]!=' ')&&(name2[a]!=' ')&&(rep>0)) {
          num++;
          name2[a]=' ';
          }
       }/*end for a*/
       rep=0;
     }/*end if*/
  }/*end for y*/
}/*end for x*/
rep2=num;
do {
  num=num-6;   }
while (num>6);
printf("\n\nThe total number of letters you both have is %d",rep2);
printf("\n\nYour compatability is: ");
if((rep2==0)&&(num==0))
  printf("Incompatible");
if((rep2>0)&&(num==0))
  printf("Friends");
if(num==1)
  printf("Friends");
if(num==2)
  printf("Lovers");
if(num==3)
  printf("Anger");
if(num==4)
  printf("Marriage");
if(num==5)
  printf("Enemy?");
if(num==6)
  printf("SSSS");
getch();
}



change it if you want....or tell me if there are bugs

This post has been edited by ungazzz: 4 Oct, 2008 - 11:02 PM
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 10:46AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month