Printf ,Scanf are functions is this correct?

This is a discussion on Printf ,Scanf are functions is this correct? within the C Programming forums, part of the General Programming Boards category; Code: // classs1234.cpp : main project file. #include "stdafx.h" #include <stdio.h> #include <string.h> #define MAX_REMIND 50 #define MSG_LEN 60 int ...

  1. #1
    Registered User
    Join Date
    Jul 2012
    Posts
    37

    Printf ,Scanf are functions is this correct?

    Code:
    // classs1234.cpp : main project file.
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <string.h>
     
    #define MAX_REMIND 50
    #define MSG_LEN 60
     
    
    int read_line(char str[], int n);
     
    
    
    using namespace System;
    
    int main(array<System::String ^> ^args)
    
      {
         char reminders[MAX_REMIND][MSG_LEN+3];
    
     
    int len=strlen(t1);
    printf("%d", len);
         char day_str[3], msg_str[MSG_LEN+1];
         int day, i, j, num_remind = 0;
    
         for (;;) {
             if ( num_remind == MAX_REMIND ) {
                printf("--No space left--\n");
                break;
             }
       
             printf("Enter day and reminder: ");
             scanf("%2d", &day);
          
             if (day == 0 )
                  break;
             sprintf(day_str, "%2d", day);
             read_line(msg_str, MSG_LEN);
    
             for ( i = 0; i < num_remind; i++ )
                  if ( strcmp( day_str, reminders[i]) < 0 )
                       break;
             for ( j = num_remind; j > i; j--  )
                  strcpy( reminders[j], reminders[j - 1] );
      
             strcpy( reminders[i], day_str );
             strcat( reminders[i], msg_str );
           
             num_remind++;
         }
    
         printf("\nDay Reminder\n");
         for ( i = 0; i < num_remind; i++ )
             printf(" %s\n", reminders[i]);
    
        return 0;
    
      }
    
      int read_line(char str[], int n )
      {
          char ch;
          int i = 0;
     
          while ( ( ch = getchar() ) != '\n'  )
              if ( i < n )
                  str[i++] = ch;
          str[i] = '\0';
          return i;
      }
    I've have about 4 good hours of practice with .c files
    I did not write the code above and barely understand
    directives,Char var, statements and so on
    the code above is the code i plan to pick apart to
    start my adventure learning C programming
    any advise as to why the code will not return any value after a keystroke . It simply ignores everything. maybe this is my
    fault by not telling it to do so.
    what should i do to get this to recognize words and so on?

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    You might want to "start your adventure" the way most us did ... one small example at a time. Get a grasp of the fundamental basics first, then grow slowly from there. Start by printing a sentence to the screen, for example.

    If you have absolutely no knowledge of or experience with C, then starting by picking this code apart is a very bad idea.

    [edit] (And yes, "printf()" and "scanf()" are functions.)

  3. #3
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    Thank you
    not bad advise however
    i have 48 hours to understand the following steps
    Create a program called stringProcessing.c that does the following:




    1. Prompt the user and accept a string of no more than 254 characters
    2. Prompt the user and accept a second string of no more than 254 characters
    3. Display the length of both strings (not including the '\n' character)
    4. Create a third string consisting of the first half of string (1) and the second half of string (2). Display this string



    can we start with something like this
    Let me highlight 2 things that i dont understand to make my question clear and easy


    1. Code:
       // clapp strings.cpp : main project file.
      
      #include "stdafx.h"
      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      using namespace System;
      
      int main(array<System::String ^> ^args)
      {
          char SingleChar;
          char mySentence[30] = "My name is Jeff"; /*can initialize a string when declared*/
      
          char yetAnotherSentence[3] = "OR";
          int intArr[30]; /* for perspective, will dwell on arrays later in class */
          
          int lenOfMySent;
      
          SingleChar = 'K';
      
          printf("The text in mySentence is [%s]\n", mySentence);
      
          mySentence[11] = 'S'; /* pick one slot in mySentence which is an array and modify */
          mySentence[9] = 'S'; /* pick one slot in mySentence which is an array and modify */
          printf("The text in mySentence after updating one alphabet is [%s]\n", mySentence);
          printf("printing mySentence from 7'th slot onwards [%s]\n", mySentence + 7);
      
          printf("scanf is a fragile way of reading a string \n \
                 1. One it doesn't read a sentence, will stop at the first space\n \
                 2. Will not check whether you've crossed the limit/ size for the string variable\n \
                 ");
          printf("\nEnter a word(no space and <=29 in size)\n\tto read into mySentence using scanf :");
          scanf("%s", mySentence); /*Look Ma, no & (in front of mySentence)*/
          printf("The text in mySentence is with value from user is [%s]\n", mySentence);
          scanf("%*c"); /*gobble up all extra space to get ready to read the next string*/
          printf("\nEnter a sentence(<=29 in size)\n to read into mySentence using fgets :");
          fgets(mySentence,29,stdin);
          printf("The text in mySentence is with value from user with the newline is \n[%s]\n", mySentence);
      
          
      
          lenOfMySent = strlen(mySentence); /* using the strlen in string.h library to get the length of the mySentence
                                            includes the newline that the user entered to indicate done entering the string
                                            but does not include the string delimitor
                                            */
      
          mySentence[lenOfMySent - 1] = '\0'; /*manually get rid of the newline that the user entered to indicate done entering the string*/
          printf("The text in mySentence now that we've taken away the end newline is\n [%s]\n", mySentence);
          printf("The length of mySentence now that we've taken away the end newline is [%d]\n", lenOfMySent);
          printf("The sizeof of mySentence (the array) is [%d]\n", sizeof(mySentence));
           printf("The sizeof of intArr (the array) is [%d]\n", sizeof(intArr));
      
      
      
          system("pause");
          return 0;  
      }
    Last edited by AutoIt Addict; 07-18-2012 at 11:51 PM. Reason: the code is a little more basic for my understanding

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    Create a program called stringProcessing.c that does the following:
    Well, you're a long way off, my friend. That is not even the correct language. That looks like C++ code, which would have a *.cpp extension.

    (And only the source code would end in .c, not the program when it's compiled.)

    48 hours? Yikes.

  5. #5
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    don't scare me like that! i think the instructor will accept any .c (or) .cpp file
    we were allowed to select our own IDE if it meets the requirements which i believe C++ does

    let me state i am allowed to submit is source code and
    are not allowed to submit .exe files
    Last edited by AutoIt Addict; 07-19-2012 at 12:16 AM. Reason: correction

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    That code bears a striking resemblance to this code.

    Code:
    char SingleChar;  // <-- declare a variable of data type "character" (typically one byte)
    // ...
    SingleChar = 'K';  // <-- assigning the character for upper-case 'K' to this variable
                       //     the single quotes indicate a single character
                       //     this is equivalent to "SingleChar = 75" as the 'K' character
                       //       on the standard ASCII chart is respresented by the number 75
    Code:
    char mySentence[30] = "My name is Jeff";
    // declare an array of 30 characters, and assign a string constant to it
    // though this array is sized for 30 characters, it only contains 16 characters
    //   (if you think it only contains 15 characters, you're forgetting that for a
    //   character array in C to be a string, it must be terminated with a null character
    //   which counts as a character of its own).
    // Each character (letter, space, etc) is stored in each ascending element of
    //   the array (starting at [0]).
    
    int lenOfMySent;
    // declare an integer variable
    
    // ...
    
    lenOfMySent = strlen(mySentence);
    // the integer assigned to "lenOfMySent" will be the length of the string contained in
    //   the array indicated by "mySentence" (minus the null character)
    Last edited by Matticus; 07-19-2012 at 12:36 AM.

  7. #7
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    So let me see if i got this right

    char SingleChar;//is the character type of the variable?
    SingleChar = 'K';// is the what the user would input to get the assigned output?

    /*but how is this equivalent to "SingleChar = 75"*/
    Code:
    char SingleChar;  // <-- declare a variable of data type "character" (typically one byte) // ... SingleChar = 'K';  // <-- assigning the character for upper-case 'K' to this variable                    //     the single quotes indicate a single character                    //     this is equivalent to "SingleChar = 75" as the 'K' character                    //       on the standard ASCII chart is respresented by the number 75

  8. #8
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    If i wanted a console app to ask the user how many apples they picked today
    user enters 8 for 8 apples
    how would i code this
    and calculate there rate of pay =1$ per apple
    then return the days pay in whole numbers on the screen (console app)

    what would this code look like?
    thanks for helping learn this

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    That's not even C++ though, It's C++/CLR. In other words, it's .NET.
    It will not compile on anything but Microsoft's Visual Studio compilers.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    2,692
    Write us a brief C program to print something to the screen, so we can see what you're capable of and work from there.

    Either that, or let desperation and panic settle in as you try to get strangers to modify borrowed code that you don't understand so it's custom tailored to your particular assignment which is due tomorrow.

    Either way, be aware that code taken from the internet might be discovered and lead to a failing grade (as in this recent case on this forum).

  11. #11
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    this thread has moved to
    Data types and tokens .cpp

    where we can conveniently talk about .cpp files
    Last edited by AutoIt Addict; 07-20-2012 at 12:35 AM. Reason: open link in the same browser not working

  12. #12
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    review
    there seems to be many shapes and sizes
    of category lib

  13. #13
    Registered User
    Join Date
    May 2009
    Posts
    2,767
    FYI: If you are just starting C/C++ programming it is NOT likely they mean you to write dynamic or static libraries.
    It is MUCH more likely that wish you to do a multiple file project with the project is divided into modules
    (some people call modules libraries; but, they are NOT the same as dynamic or static libraries).
    The modules in this case are likely to be two source files; one being a header file and the other being an .c (for C) or .cpp (for C++) file.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  14. #14
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    //looks like you know something about modules thank you sir
    if i where to run with my app
    what would you say
    Last edited by AutoIt Addict; 07-21-2012 at 05:39 PM.

  15. #15
    Registered User
    Join Date
    May 2009
    Posts
    2,767
    Quote Originally Posted by AutoIt Addict View Post
    //looks like you know something about modules thank you sir
    if i where to run with my app
    what would you say
    I would say you need to clearly ask a question.
    Because, I have no idea what you are asking.

    Tim S.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help. printf() and scanf().
    By HBK in forum C Programming
    Replies: 19
    Last Post: 07-17-2011, 05:04 PM
  2. printf and scanf
    By silentintek in forum C Programming
    Replies: 1
    Last Post: 10-27-2008, 10:32 PM
  3. Seemingly correct printf statements bringing up errors
    By hpteenagewizkid in forum C Programming
    Replies: 9
    Last Post: 11-08-2006, 12:13 PM
  4. printf() and scanf()
    By homeyg in forum C Programming
    Replies: 1
    Last Post: 02-06-2006, 10:43 PM
  5. scanf and printf
    By gmanUK in forum C Programming
    Replies: 5
    Last Post: 11-25-2005, 03:03 PM

Tags for this Thread


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