Thread: Need help understanding input charakter.

  1. #1
    Registered User
    Join Date
    Jul 2007
    Location
    Bandung-West Java-Indonesia
    Posts
    2

    Unhappy Need help understanding input charakter.

    I am stil very baby in c++ programming and now i try to make a simple code for my first small programm. This programm was called your_astro. In this programm i want to make a simple astrology information about someone's fortune. So, when someone type a name of astrology, i expect someone will see the information about the astrology that have he/she typed. I made this programm on Ubuntu Linux using c++ compiler gcc version 4.1.2..

    I have compiled the source sucsessfuly using comand
    Code:
     g++ my_astro.cpp -o my_astro
    when i run it, first there are no errors, this is the display when i run it at first
    Code:
    Welcome to my super simple application.
    Type your Astro: (aries, pices, libra) just three astro
    the problem is, when i type one of the three astros and than hit enter, there are an information which says that
    Code:
    Segmentation fault (core dumped)
    . I still don't know what its mean. So help me to solve this problem, please. How the true source code is.
    Your help please, and thaks for your help before.
    I'm so sory if my English is bad. ^_^

    This is the source code.
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main() {
    char* welcome="Welcome to my super simple application.";
    char* type="Type your Astro: (aries, pices, libra) just three astro"; /*just 3 astro only now*/
    char* did_not_match="Type correctly please, there are just 3 astro";
    char* a="aries";
    char* b="pices";
    char* c="libra";
    char* A;
    
    cout<<welcome<<endl<<type<<endl;
    
    cin>>A;
    
    if (A=a){
    	cout<<"Your astro is ARIES. You are blablabla..."<<endl;
    	} 
    	
    	else{
    	cout<<did_not_match<<endl;
    	}
    /* I have not make the next code for the if case above yet.*/
    
    return 0;
    }

  2. #2
    Ethernal Noob
    Join Date
    Nov 2001
    Posts
    1,901
    Use the <string> library rather than c-style strings. It probably has to do with cin >> A;

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    = is for assignment, == is to use when testing for equality. Even if you get the issue with obtaining user input into a char* resolved, you would still not be able to compare for equality using == because in that kind of context, you'd only be comparing if the two address's were the same and not the contents. That is why using a string is much easier to deal with:
    Code:
    #include <string>
    
    ...
    
    string str;
    
    cout << welcome << endl << type << endl;
    
    cin >> str;
    
    if (str=="Aries"){
    You could use your code but you would first have to assign some memory to the pointer A, either by using dynamic memory allocation (new/delete) or pointing A to an existing character buffer, and then use the strcmp function instead of testing for equality using ==.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jul 2007
    Location
    Bandung-West Java-Indonesia
    Posts
    2

    Smile Thanks -- it sucsess

    Thanks. I had try it and it done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. input redirection
    By sashaKap in forum C Programming
    Replies: 6
    Last Post: 06-25-2009, 01:59 AM
  2. continues input
    By Matty_Alan in forum C Programming
    Replies: 2
    Last Post: 06-22-2007, 10:04 PM
  3. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  4. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM