Thread: Help!

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    1

    Exclamation Help!

    help with code
    the user is suposed to type

    PRINT EXAMPLE

    AND IT SHOULD PRINT
    EXAMPLE
    Code:
    #include "conio.h"
    #include "stdio.h"
    int main()
    {   
    int array[100],i; 
      printf("array[]:"); 
      scanf("%s",&array);  
     if(array[0]=='P')   {  while (array[i]>1)   
    {  
     printf("%u",array[i]);   i++; 
      }  
      }   getche(); 
      return 0;}
    Code tags added by kermi3

  2. #2
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    ugg put some code tags on that mess

    and a %s code in scanf expects an argument of type

    char *

    but you give it an argument of type

    int **
    Last edited by moi; 10-07-2002 at 06:49 PM.
    hello, internet!

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happy about helping you


    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found at the link in my signature. Any further questions or ways I can help please feel free to PM me.

    I hope this helps, and good luck,

    Kermi3
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int array[100],i; 
        printf("array[]:"); 
        scanf("%s",&array);  
        if(array[0]=='P')   {
            while (array[i]>1)   
            {  
                 printf("%u",array[i]);   i++;
    So what you have is:

    for( i = some random number; i > 1; i++ )

    Does this make sense to you? Do you see why this wouldn't work?

    1) You never initialize 'i', so it has some totally random value.
    2) You check to make sure 'i' is greater than 1, and if so...
    3) You INCREMENT 'i'??

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by quzah
    So what you have is:

    for( i = some random number; i > 1; i++ )

    Does this make sense to you? Do you see why this wouldn't work?

    1) You never initialize 'i', so it has some totally random value.
    2) You check to make sure 'i' is greater than 1, and if so...
    3) You INCREMENT 'i'??

    Quzah.
    well, assuming some 100% random value is stored in i, 50% of the time it will be negative, so nothing to worry about; if it doesnt work just run it again!

    further elaboration on the scanf cj1:

    when you use the %s scanf code it will read a string into a char array. you have declared an int array, and then you take the address of a pointer to the first element in that array. no. i don't even want to think about the possible mishaps of running this

    try:

    char array[100];
    scanf ("%s", array); // or &array[0]
    hello, internet!

Popular pages Recent additions subscribe to a feed