Saturday, May 22, 2010

Any one knowing C++? what is the problem with this program?

Hi,friends!


I have written a program but it doesn't work!


can you find the problem?





#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


int main()


{


clrscr() ;





char a,b,c,n,y,g,d,f ;


cout%26lt;%26lt;"Hi do you want to have a chat? (y/n)" ;


cin%26gt;%26gt;a ;


if (a=y)


{


cout%26lt;%26lt;" My name is Computer.what is yours? " ;


cin%26gt;%26gt;f ;


cout%26lt;%26lt; "Nice to meet(!) you" ;


cout%26lt;%26lt;f ;


cout%26lt;%26lt;"Are you a girl or a boy? (b\g) " ;


cin%26gt;%26gt;c ;


if (c=g)


{


cout%26lt;%26lt;"Have a nice day! Ms "


cout%26lt;%26lt;f ;





}


if (c=b)


{


cout%26lt;%26lt;"Have a nice day Mr. " ;


cout%26lt;%26lt;f ;


}


}


if (a=n)


{


cout%26lt;%26lt;"Good bye then!" ;


}


getch() ;


}











when It should write you name(f).It just writes the first letter.


and it writes all the "if" parts together whan i enter b or g.





Thanks for any help!


Have a nice day!

Any one knowing C++? what is the problem with this program?
%26gt; #include %26lt;iostream.h%26gt;





Change to %26lt;iostream%26gt; The .h form for standard headers is deprecated.





Also, right after the includes, add in a " using namespace std; ". That way you can skip the std:: namespace qualifier. Eventually, you should stop using the using namespace std statement though.





%26gt; #include%26lt;conio.h%26gt;





Get rid of conio.h. Try not to use it in your programs because it isn't part of standard C++.





%26gt; clrscr() ;





Get rid of it. Part of conio.h





%26gt; char a,b,c,n,y,g,d,f ;





Ok, here's a big hint about variables names. They can be more than one letter long. What is a? b? c? n? y? g? d? f? This isn't a secret code here. You need to give meaningful names like userName, userAge, userGender, wantChat, and so on.





Second, char is only *one* of the possible primitive data types in C++. Let's look at some of the others you have. bool (boolean), int (integer), float, char, string (if you #include %26lt;string%26gt;) and so on. Make use of them when appropriate!





For example, when you ask a person for his name, what datatype do you use? Char? Think about it. How long is a name? For example, Mike. That's four letters. How many letters does a char have? 1. Just 1. Oops. Looks like a char isn't good enough for a name. You want a string (many characters). #include %26lt;string%26gt; and then create a string variable in your program.





%26gt; if (c=g)





= is for assignment. == is for comparison. so you want if (c==g) which compares c with g. if you do if ( c =g ), all it does is make c equal to g, and then returns true.





%26gt; if (c=b)





Same here.





%26gt; if (a=n)





And here.





* Make your variable name more meaningful.


* You need a string to hold a name.


* Also, you have no newlines in your output. You realize everything will be on one line?
Reply:C++ String are just char array, so if you only put f, then it will only print the first character (similar to java if you write string[0]), you need to indicate the pointer *f instead.
Reply:your "if" statements are wrong!





it's if(a == 'y') not if(a=y)





the char y that you declared is uninitialized, and you used an assignment operator (=) rather than the comparison (==)





in you first if, you are assigning whatever garbage is in the uninitialized y variable to a, rather than comparing a to a specific value 'y'








the same happens for the rest of your if() clauses
Reply:if (a=y) this is not correct this line should be if (a==y) and all conditions must be modified in this way....


http://www.findindirectory.com/fallin.as...


No comments:

Post a Comment