Howdy, Stranger!

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

Categories

Help with these strings:

char array[20]=my_age;

char string1[20]=my_age;


CAN YOU GIVE ME THE CONVERSION SPECIFIER ( % ) FOR THE CHAR TYPE? example: for integer its %d --what about the char type? float: %f--what about the char type?

guess: %c ??? (or that only gives the one character?

please tell me the exact conversion specifier so I can make this code more better and in more detail after compiliation.

or they both the same or they are different? : is one an array of char, and the other one is a string of char? or they both the same or what? my_age is an char type (variable) that holds one byte that will nicely fit into the array / or string?



Can this be compiled?


#include <<iostream>>.h

void main()


char name[20];

char lastname[30];

char age[100];

puts("Enter your name";

gets(name) ;

if (name > 20 ) ;

puts("Too many characters
"; /*take out newline?*/

else

puts("Thanks for your name";

puts("Enter your lastname";

gets(lastname) ;

if (lastname > name ) ;

puts("Your lastname is bigger";

else

puts("Your first name is bigger";

puts("Enter your age in a full sentence";

gets(age) ;

puts("Its not in a sentence";

else

puts("Thanks for the sentence";

if (age > 100 ) ;

puts("Wow, your age!";

else

puts("Your younger!";

printf(%c
", age ) ; /*%c--? */prints single character?,'age' is a whole character/*



#define (age) ; /*whatevers the users age?, or 100);its is 'ages' variable(all char) /*


char array[2]=age, name, lastname ) ;

array[0]=100;

array[1]=20;

array[2]=30;


/*is that a true array? if not, give me the real code for it /*


is this the one right? :


char array[2]=100, 20 30 ) ;

array[0]='age';

array[1]='name';

array[2]='lastname';


for (age=100; (age > 2 ) age++ ) ;

break;

for (name=20; (name > 2) name++ ) ;

break;


if intcmp (age > name ) ;

printf(%c
", age, name ) ; /*%s used for char? or single character? again, give me the conversion specifier/*


correct the above code and give me it the right away block by block if (some) parts are incorret.

Please answer this important question:

Is the conditional part of the for loop:

name > 2 ); 2 is the dimension of the array (the number in the bracket) is that what you put in a for loop? have to declare a char array for this type char stuff before the for loop?


Strings: why do we need strings when we got arrays? (I cant find strings useful) what are they used for? like I did the code above, the start of it as I am passing the gets call to get the users name into my program,(is that the only field where the strings are useful( but that is not the kind of string that uses comparsions and lenght functions from <<string>>.h

Its just a normal character array(is it a string) or if the users typed in a long string in the GETS area at the start of my code , it will al go in the character array holding that much memory for it?

or if the users typed in a long string passed the value of the char array 'name' , will an error come?

or do I have to increase the value of the first char array? or is it a string?

Or should I use 'else' like I did above, to have the two freedom ways from the user, incase the user typed in something long that I did not programmed it for(a long string) , an error will come? this is something similiar:

char introduce[100];

puts("introduce yourself";

gets(introduce) ;

puts("Thanks for your info";

_______________________________________________________







Comments

  • Get a C book. You're throwing together variable types and keywords and standard functions almost willy-nilly. It's almost impossible to determine what you do and don't know from wandering through this.




    : char array[20]=my_age;

    : char string1[20]=my_age;


    You cannot do the above two lines unless my_age happens to be a #define'd string literal. If you don't know what that means, don't bother. You can only initialize character arrays with string literals like:


    "Hello"


    or with an array initializer list:


    {'H','e','l','l','o',0}




    You can, of course, simply do a strcpy:


    strcpy(array,"Hello");



    : CAN YOU GIVE ME THE CONVERSION SPECIFIER ( % ) FOR THE CHAR TYPE? example: for integer its %d --what about the char type? float: %f--what about the char type?

    : guess: %c ??? (or that only gives the one character?


    %c is the C conversion code for the scanf and printf family of functions. It describes the input or output of a single character. If you're looking for reading in or outputting strings, consider %s.




    : please tell me the exact conversion specifier so I can make this code more better and in more detail after compiliation.

    : or they both the same or they are different? : is one an array of char, and the other one is a string of char? or they both the same or what?




    An array of characters _can be_ a C style string as long as it is null terminated. All of the C string manipulation and I/O routines assume that if you give it a character array to work with then it is a C-style string.


    NOT ALL CHARACTER ARRAYS are C STYLE STRINGS. If the data is not null terminated, then it isn't a C Style string. Input routines and manipulation routines will append a null character at the right place for you.




    :my_age is an char type (variable) that holds one byte that will nicely fit into the array / or string?




    A single char like my_age can be assigned to one of the _elements_ of a char array, or an element of the char array can be assigned to a single char. I guess in that sense it 'fits'.


    C isn't just about 'fitting things'. Damn near everything is 'typed'. In order for one variable to be assigned to another, or for a variable to be assigned to a constant, or for a variable to be passed to a function, the types must match, or there must be a 'conversion' that the compiler knows about.


    :

    : Can this be compiled?


    : #include <<iostream>>.h

    : void main()


    : char name[20];

    : char lastname[30];

    : char age[100];

    : puts("Enter your name";

    : gets(name) ;

    : if (name > 20 ) ;


    'name' as a C style string cannot be compared with anything like this. String comparisons must be done with the function strcmp. String length can be found with strlen. If you want to do numerical comparisons, then strings aren't what you want. Use an int or a double variable and use scanf to read into it.


    The semicolon at the end of your if statement says to the compiler 'if this is true, do nothing'. There is no semicolon after an if.




    : puts("Too many characters
    "; /*take out newline?*/




    Missing a close parenthesis.


    : else

    : puts("Thanks for your name";


    Missing a close parenthesis.


    : puts("Enter your lastname";


    Missing a close parenthesis. (get the point yet?)


    : gets(lastname) ;

    : if (lastname > name ) ;


    Can't do this comparison. It will compile, but it will compare the addresses of the strings, which is probably not what you want. Semicolon means do nothing here.




    : puts("Your lastname is bigger";


    Missing a close parenthesis.


    : else

    : puts("Your first name is bigger";


    Missing a close parenthesis.


    : puts("Enter your age in a full sentence";


    Missing a close parenthesis.


    : gets(age) ;

    : puts("Its not in a sentence";




    Missing a close parenthesis.




    : else


    else is not attached to a previous if.




    : puts("Thanks for the sentence";




    Missing a close parenthesis.




    : if (age > 100 ) ;


    Illegal comparison of a character array and a number. semicolon again means do nothing.




    : puts("Wow, your age!";


    Missing a close parenthesis.


    : else

    : puts("Your younger!";


    Missing a close parenthesis.


    : printf(%c
    ", age ) ; /*%c--? */prints single character?,'age' is a whole character/*




    Missing an open doublequote. age is an array of characters, not a single character, which %c tries to print out.


    :

    : #define (age) ; /*whatevers the users age?, or 100);its is 'ages' variable(all char) /*




    #define is a preprocessor macro. If you don't know how to use it, don't. It'll make things worse. I'm not even going to try to explain them here.




    : char array[2]=age, name, lastname ) ;




    This just makes no sense at all. I think you're trying to initialize a character array, but I can't even imagine what your intention is.




    : array[0]=100;

    : array[1]=20;

    : array[2]=30;




    This *will* work.


    : /*is that a true array? if not, give me the real code for it /*


    Your C comments MUST terminate with a */, not with a /*.




    : is this the one right? :


    : char array[2]=100, 20 30 ) ;


    If you want to initialize an array with an initializer list you must contain the list in curly braces, not parenthesis, which you only have one of anyway.




    : array[0]='age';

    : array[1]='name';

    : array[2]='lastname';




    You're trying to assign to an element of 'array' (which is of type 'char') a multicharacter constant which isn't legal in C anyway, and even if it were, wouldn't be allowed in this assignment.



    : for (age=100; (age > 2 ) age++ ) ;


    Missing a semicolon inside the for statement. Even if it were there, this would do nothing since the semicolon is at the end and it would immediately fail the comparison you've given it (it's already greater than 2).


    : break;


    This does nothing here.




    : for (name=20; (name > 2) name++ ) ;


    See above loop.




    : break;




    Again this does nothing.

    : if intcmp (age > name ) ;


    There is no standard function called 'intcmp', and you're passing to it a true or false (whether or not the comparison passes or fails). The semicolon at the end again means 'do nothing'.




    : printf(%c
    ", age, name ) ; /*%s used for char? or single character? again, give me the conversion specifier/*




    You're attempting to output a character but you're giving it an array of characters. This doesn't work. If you want to output a character, give it a character.




    : correct the above code and give me it the right away block by block if (some) parts are incorret.




    No. I have no idea what you're intentions were in writing this code, so I'm not going to speculate.




    : Please answer this important question:

    : Is the conditional part of the for loop:

    : name > 2 ); 2 is the dimension of the array (the number in the bracket) is that what you put in a for loop? have to declare a char array for this type char stuff before the for loop?




    You can use the variable that the for loop is using for any other purpose you like. Use it as an array index, add it to some other variable, whatever. All the comparison does is either allow you into the loop or not based on whether the comparison passes or fails.



    : Strings: why do we need strings when we got arrays? (I cant find strings useful) what are they used for?




    See above comment about strings and character arrays.




    :like I did the code above, the start of it as I am passing the gets call to get the users name into my program,(is that the only field where the strings are useful( but that is not the kind of string that uses comparsions and lenght functions from <<string>>.h


    I have no idea what you are meaning by a 'string' then. A C style string is an array of characters terminated by a null byte. If you want to use the array of characters in your own way, that's fine, but then it is an array of characters and C-style-string routines won't work on it.




    : Its just a normal character array(is it a string) or if the users typed in a long string in the GETS area at the start of my code , it will al go in the character array holding that much memory for it?




    gets will place into a character array a c-style string based on the user's input. It is dangerous in that it *doesn't* check the amount of space you've reserved. That's why no one in their right mind would use the gets routine in production code. For experimentation, you're probably just fine using it.



    : or if the users typed in a long string passed the value of the char array 'name' , will an error come?




    No error will come from entering a string too long when using gets to read in the data.




    : or do I have to increase the value of the first char array? or is it a string?




    It is a character array. It is a string if you always use it as a string.




    : Or should I use 'else' like I did above, to have the two freedom ways from the user, incase the user typed in something long that I did not programmed it for(a long string) , an error will come? this is something similiar:




    You can use else anywhere you like provided it has a preceeding 'if'. If you want to not overflow your array with gets, then you'll have to read data in character by character and put each character in the array yourself at an appropriate spot.





    : char introduce[100];

    : puts("introduce yourself";

    : gets(introduce) ;

    : puts("Thanks for your info";

    : __




    Again, you're missing closing parenthesis.


    You must be much more careful with your coding. Computer languages are very very precise.




Sign In or Register to comment.