Thread: this doesn't seem to work

  1. #1
    Registered User
    Join Date
    Feb 2004
    Posts
    9

    this doesn't seem to work

    a while ago someone helped me make a pig latin program. I took his suggestions and created myself. I'm trying to make an easter egg so that when you enter my girlfriends name it outputs "I love you". For some reason, when i put her name in, it seems to skip over the if statement i used? It compiled fine. Can someone tell me what's wrong with this?
    Code:
    #include<iostream.h>
    
    
    main()
    {
     while (1)
     {
     char a,b,last;
     char name[255];
     cout<<"Enter a name ";
     cin>>a;
     cin>>last;
     cin>>name; 
     if (a=='R'&&last=='e'&&name=="ggie")
     {
     cout<<"I love you";
     }
     cout<<last<<name<<a;
     cout<<"ay\n";
     cout<<"Do you want to continue? Y or N ";
     cin>>b;
     if (b=='N' || b=='n')
      {
      return 0;
       }  
      }
     }

  2. #2
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Change char name[255]; to string name;
    Otherwise you have to use strcmp()

    edit: dont forget to include this
    Code:
    #include <iostream>    // replaces iostream.h
    #include <string>
    
    using namespace std;
    Last edited by alphaoide; 02-29-2004 at 08:43 PM.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  3. #3
    Registered User
    Join Date
    Feb 2004
    Posts
    9
    if i do that, it doesn't compile

    edit//nevermind, i just saw your edit

  4. #4
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164

    Re: this doesn't seem to work

    Code:
     if (a == 'R' && last == 'e' && name == "ggie")
    You are using c-strings, you can't use ==, use strcmp()
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  5. #5
    Registered User
    Join Date
    Feb 2004
    Posts
    9
    when i compile, its saying "'string' undeclared"

    and can i use strcomp to compare the other two variables as well, or just the strings. maybe the way i wrote the program isn't really conducive to something like this. . .

  6. #6
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    This compiles
    Code:
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    
    main()
    {
      while (1)
      {
        char a,b,last;
        string name;
        cout<<"Enter a name ";
        cin>>a;
        cin>>last;
        cin>>name; 
        if (a=='R'&&last=='e'&&name=="ggie")
        {
          cout<<"I love you";
        }
        cout<<last<<name<<a;
        cout<<"ay\n";
        cout<<"Do you want to continue? Y or N ";
        cin>>b;
        if (b=='N' || b=='n')
        {
          return 0;
        }  
      }
    }
    You got some weird design, but anyways
    >> and can i use strcomp to compare the other two variables as well, or just the strings.
    The other two are just a char; leave it like that
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  7. #7
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    > You got some weird design.

    Agreed

    > main()

    int int int main()

    - SirCrono6
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    9
    yea i know its a weird design but i'm very new at this

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by k_rock923
    yea i know its a weird design but i'm very new at this
    I suspect that you're sources are old; time to go shopping to the bookstore.
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM