STRINGS - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

STRINGS

char string1[4]="hi";

char string2[4]="bye";

if strcmp (string1, string2 >1 ) ;

else

printf(%s string1, %s string2
")


Please correct the above code.


will this compile? :


void main()


int my_age;

my_age=100;

cout<<"Input your age";<br>
cin<<age;<br>
if (my_age > 20 ) ;

cout<<"Too high number";<br>
else

cout<<"Thanks
";<br>
cout<<"I am <<my_age>> years old
";

cout<<"A year passes";<br>
my_age++

cout<<"How old are you next year?";<br>
cin>>my_age;

if (my_age > 21 ) ; /*increased by one/*

cout<<"Again, too high";<br>
else

cout<<"Thanks for your age";<br>
int *pointer;

pointer=&my_age;

*pointer=21;

/*its now 21 in 'my_age's value cause been incremented/

scanf(%d &my_age );

printf(%d my_age );

char describe[200];

puts("Now describe yourself
";

gets(describe) ;

puts("Thanks
";

Where does the users text go?

Yeah, I know, I know, nothing is correct, by I read

tutorials and books and saying that is that, and you are confusing me. This is in books:

if (expression)

(statement) ;

the if statement is the true of false or something,the statement is the cout or printf something, and you are saying that 'else' goes right after the expression, no I saw in the books that that is the layout, or is it only in C++ ? Give me beginner tips, and explain how I can fix the problem next time, instead of just telling me the problem, let me fix it.


Comments

  • General 'beginner' comments are at the bottom.




    : char string1[4]="hi";

    : char string2[4]="bye";

    : if strcmp (string1, string2 >1 ) ;


    Missing open parenthesis after the if, and a closing one after the comparison. The comparison is in the wrong spot if you want to test the return value of strmp. The final semicolon says that the code to execute if the comparison is true is empty.


    I presume you want something like:


    if (strcmp(string1,string2)>1)


    if you're going to string-compare string1 and string2. The 'if' syntax MUST be:


    if (condition)

    codeiftrue;

    else

    codeiffalse;


    Although the 'else' and its associated code can be left out.

    : else

    : printf(%s string1, %s string2
    ")


    Missing open double quote. Missing semicolon.




    : Please correct the above code.


    : will this compile? :


    : void main()


    : int my_age;

    : my_age=100;

    : cout<<"Input your age";<br>
    : cin<<age;<p>
    The cin object doesn't have a << operator. It has a >> operator.




    : if (my_age > 20 ) ;

    : cout<<"Too high number";<br>
    : else

    : cout<<"Thanks
    ";<p>


    The if statement is not properly formed.




    : cout<<"I am <<my_age>> years old
    ";


    This line will compile, but it will not print out the age. It will print out precisely what you have in the quotes. I presume you probably want:


    cout << "I am " << my_age << " years old" << endl;<p>


    : cout<<"A year passes";<br>
    : my_age++


    Missing semicolon.


    : cout<<"How old are you next year?";<br>
    : cin>>my_age;

    : if (my_age > 21 ) ; /*increased by one/*

    : cout<<"Again, too high";<br>
    : else

    : cout<<"Thanks for your age";<p>


    Badly formed if statement again. It's due to this semicolon after the if condition. Also, again, your C comments must be paired /* */, which you aren't doing.


    : int *pointer;

    : pointer=&my_age;

    : *pointer=21;

    : /*its now 21 in 'my_age's value cause been incremented/




    Hey, bingo! This use of a pointer is perfectly valid. You've both pointed the pointer at something and then dereferenced it to get at the underlying variable. Well done.


    : scanf(%d &my_age );

    : printf(%d my_age );




    Missing quotes around the %d, missing a comma separating parameters.


    By the way, if you're using cin and cout C++ streaming objects, you don't need to be using scanf and printf. Choose one style or the other. Not only will it be less confusing, but some compilers don't like it if you mix C and C++ style input/output.


    Both C and C++ come with their own unique mechanism for input and output and neither is more powerful than the other really.


    : char describe[200];

    : puts("Now describe yourself
    ";

    : gets(describe) ;

    : puts("Thanks
    ";




    Missing close parenthesis.


    : Where does the users text go?


    Into the character buffer you gave it, null terminated (C style string).




    : Yeah, I know, I know, nothing is correct, by I read


    Actually, the code is getting better. Just be very careful. You must dot all your i's and cross all your t's.




    : tutorials and books and saying that is that, and you are confusing me. This is in books:

    : if (expression)

    : (statement) ;

    : the if statement is the true of false or something,the statement is the cout or printf something, and you are saying that 'else' goes right after the expression, no I saw in the books that that is the layout, or is it only in C++ ?


    As I mentioned before, there are two forms of the 'if' construct.


    if (condition)

    codeiftrue;


    is one of them. The other is:


    if (condition)

    codeiftrue;

    else

    codeiffalse;




    Each code portion can either be a single statement followed by a semicolon, or a code block (several statements) surrounded by {} curly braces. It doesn't have to do input and output. It can be anything. But your use of it is getting better. Be careful of this:


    if (condition)

    ;




    What this says is if the condition is true, do nothing. You do this several times in your code above, but like this:


    if (condition) ;




    That is absolutely the same. C doesn't care what line things are on.




    :Give me beginner tips, and explain how I can fix the problem next time, instead of just telling me the problem, let me fix it.




    I'm doing my best. Here's some basic suggestions.




    Always balance your parentheses (), curly braces {}, and square brackets [].


    Always pair double quotes "", and single quotes ''.


    Always balance your C style comments /* */


    The conditional following an if must be wrapped in at least one pair of parenthesis.


    A semicolon on its own is a legal (do-nothing) C statement. Be aware of where semicolons do and do not go.


    A character array is not necessarily a C-style string unless you always use it as a C-style string.


    Although this editor doesn't seem to like indentations some times, consider indenting blocks of code. That is, the code for the true condition of an if construct... try indenting it one level from the if statement. It makes your code easier to read.


    C is a very typed language (though not as much as C++). This means that, unless the compiler can find an acceptable conversion on its own, a variable of one type cannot be automatically assigned to a variable of another type. It also means that a pointer to one type cannot be automatically pointed at a variable of another type. Further, it means you cannot automatically pass to a function a variable of one type when the function is expecting another type.


    Types are important. Know the types of your variables.


    Computer languages (and C is no exception) are very very very very very very picky about syntax. You _cannot_ miss out a period or a semicolon or an open-parenthesis when the compiler is expecting it. I'm not kidding. Don't fool yourself. You won't fool the compiler.





Sign In or Register to comment.