scanf and pointer

This is a discussion on scanf and pointer within the C Programming forums, part of the General Programming Boards category; Hello there, I have a problem in C with pointers. I want to a input data check in my main ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    66

    scanf and pointer

    Hello there,

    I have a problem in C with pointers.
    I want to a input data check in my main function. I use a function to do that job. But I do not the syntax for pointer format.

    look:

    Code:
         float wmin, wmax, dist, r;
         int first,sec;
         
         printf(" Dwse Wmin: ");
         scanf("%f", &wmin);
         
         printf(" Dwse Wmax: ");
         scanf("%f", &wmax);
         InputCheck(wmin,&wmax);
         cout << wmax;
    Now, I want to check if wmax is bigger than wmin.

    so here is my function to do that:
    Code:
    void InputCheck(float wmin, float *wmax)
    {
         while ( *wmax < wmin){
               printf("\n\nERROR!!!\n\nTo Wmax prepei na einai megalutero to Wmin !!!");
               printf(" Dwse Wmax: ");
               cin >> *wmax; // this works in C++ but I want the same command in C
         }
    }
    Please replace my C++ command with another one in C.
    Thanx!

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > Please replace my C++ command with another one in C.
    Do it yourself, research printf().

    > Now, I want to check if wmax is bigger than wmin.
    > while ( *wmax < wmin)
    You probably want if(*wmax > wmin)

    For 1, that's wmax is less than wmin, not if wmin is greater than wmax. The loop is pointless, for what you want to do.

    This really has nothing to do with pointers.
    Last edited by zacs7; 04-29-2008 at 05:46 AM.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    This works perfect for me.

    Code:
    #include <stdio.h>
    #include <iostream>
    #include <math.h>
    using namespace std;
    
    void InputCheck(int *data, int n)
    {
         while( *data > n)
         {
                     printf("\n. ERROR!!!!\n\nDialekse ton sosto radio station: ");
                     cin >> *data;
         }
    }
    
    void InputCheck(float *wmin, int min)
    {
         while( *wmin >= min)
         {
               printf("\n\nERROR!!!\n\nTo Wmin prepei na einai pio mikro apo to %d !!!", min);
               printf(" Dwse Wmin: ");
               cin >> *wmin;     
         }
    }       
    
    void InputCheck(float wmin, float *wmax, int max)
    {
         while ( *wmax < wmin ){
               printf("\n\nERROR!!!\n\nTo Wmax prepei na einai megalutero to Wmin !!!");
               printf(" Dwse Wmax: ");
               cin >> *wmax;
         }
         
         while (*wmax <= max ) {
               printf("\n\nERROR!!!\n\nTo Wmax prepei na einai megalutero to %max !!!", max);
               printf(" Dwse Wmax: ");
               cin >> *wmax;
         }
               
    }
               
    
    void check(float pinakas[], int n, int min, int max)
    {
         float wmin, wmax, dist, r1, r2;
         int first,sec;
         
         printf(" Dwse Wmin: ");
         scanf("%f", &wmin);
         InputCheck(&wmin, min);
         
         printf(" Dwse Wmax: ");
         scanf("%f", &wmax);
         InputCheck(wmin,&wmax,max);
        
              
         printf(" Dwse apostasi: ");
         scanf("%f", &dist);
         
         // Dialekse 2 radiostathmous
         printf("Dialekse ton 1o radio station: ");
         scanf("%d", &first);
         InputCheck(&first,n);
        
        
         
         printf("Dialekse ton 2o radio station: ");
         scanf("%d", &sec);
         InputCheck(&sec,n);
         
         r1 = sqrt(pinakas[first]/wmin);
         r2 = sqrt(pinakas[sec]/wmin);
         cout << r1 << " " << r2;
         
         
         
    }
    
    
    
    
    
    main()
    {
          int n,i,min,max;
          float w;
          
          printf(" Posoi einai oi Radiostathmoi? : ");
          scanf("%i", &n);
          
          // dunamikos pinakas
          float *pinakas = new float [n];
          
          
          for(i=0; i<n; i++)
          {
                   printf("Dose tin suxnotita tou %d, radiostathmou: ", i+1);
                   scanf("%f", &pinakas[i]);
    
          }
          
          min = pinakas[0];
          max = pinakas[0];
          for(i=0; i<n; i++)
          {
                   if(min > pinakas[i])
                   {
                          min = pinakas[i];
                   }
                   
                   if( max < pinakas[i])
                   {
                       max = pinakas[i];
                   }
          }
          
          
          
          
                   
          
          check(pinakas,n, min,max);
          
          
          
          system("PAUSE");
          return 0;
    }
    All I want is to know how to use scanf with pointers. If you take a loot at the source code, I use the cin command. The problem is that I want to save my project as *.c and not as a *.cpp


    Thank you

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    So what's wrong with
    scanf( "&#37;f", wmax );

    wmax is a pointer to a float, scanf wants a pointer to a float - luvvly jubbly
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > If you take a loot at the source code, I use the cin command.
    And a lot of other C++, it's not as simple as 'renaming it to *.c'.

    Go and research:
    * main() in C -- over 'C++' main
    * malloc()/free() -- over new/delete
    * printf()/scanf() -- over cout/cin

    And of course there is no function overloading in C.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. Replies: 5
    Last Post: 03-23-2009, 03:44 PM
  3. char pointer working in scanf but not in cin.
    By sawer in forum C++ Programming
    Replies: 14
    Last Post: 06-15-2006, 02:15 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  5. scanf to struct pointer
    By ronenk in forum C Programming
    Replies: 11
    Last Post: 12-20-2004, 09:22 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21