Like Tree3Likes

Data types and tokens .cpp

This is a discussion on Data types and tokens .cpp within the C++ Programming forums, part of the General Programming Boards category; Matticus yes the code was given to me by the instructor and if you would kindly read what white flags ...

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

    yes the code was given to me by the instructor and if you would kindly read what white flags has wrote this may clear
    up some of your confusion
    I have no plans to copy internet code as that would not work anyway
    the assignment is fairly simple program if you have a basic understanding of strings arrays tokens int and variables
    as for now i plan to see if i can learn to use c source in code blocks
    then i will try to clean up the messy code that seems to be a mix of c and C++
    however if the instructor gives me example code that has c and c++ there is not much i can say about that other than
    take white flags advise above

  2. #17
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,549
    The code is clearly C with C++/CLI added in. C++/CLI is not C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #18
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    Matticus

    looks like where in business code blocks allows you to write in c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        printf("Hello world!\n");
        return 0;
    }
    this file has a .c extension do you see any c++ code here?

  4. #19
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    That is pure C. Are you still interested in programming the assignment you described in the other thread? If so, do you know how create a string in C?

  5. #20
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    yes i am interested
    dont yet know how to create a sting in c
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
        printf("Hello world!\n");
        return 0;
    }
    Last edited by AutoIt Addict; 07-20-2012 at 02:26 PM.

  6. #21
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    Well.. no better time to start learning !
    C Tutorial - Learn C - Cprogramming.com
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  7. #22
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    Do you know how to create an array of variables?

    A string is simply an array of characters, terminated with a null character '\0'.

    For instance, this code will declare a character array with 6 elements:

    Code:
    char cArray[6];
    Each element can contain a character, and are indexed from zero to "n-1"

    Code:
    cArray[0] = 'H';  // <--- note a single character is contained within single quotes
    cArray[1] = 'e';
    cArray[2] = 'l';
    cArray[3] = 'l';
    cArray[4] = 'o';
    cArray[5] = '\0';
    
    // There are six elements in this array, numbered 0 - 5.
    // Some new to this mistakenly try to access "cArray[6]" - ** this is out of bounds! **
    There are other ways to define a string. For instance, we could have done this:

    Code:
    char cArray[6] = "Hello";  // <--- note string of characters is contained within double quotes
    Note that "Hello" only has five characters, but our array has six elements. This is because we need to leave space at the end of the string for the null character (which is appended automatically when a string is declared like this).

    Play around with this for a little bit until you feel comfortable, then we can go over the next step.

    [edit] Perhaps I should include some printing techniques:

    Code:
    // printing one character at a time
    
    for(i=0; i<6; i++)
        printf("%c",cArray[i]);  // <--- %c prints a single character
    Code:
    // print entire string at once
    
    printf("%s",cArray);  // <--- %s prints a null-character-terminated string
    // note that just the array name is given without any brackets/indices
    Last edited by Matticus; 07-20-2012 at 02:41 PM.
    rags_to_riches likes this.

  8. #23
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    ok give me some time to play with this and i will get back to you in hour or so

    thanks

  9. #24
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    -k-
    got it now
    a string is a collection of characters otherwise defined as array

  10. #25
    Registered User
    Join Date
    Jul 2012
    Posts
    37

    its printing single char

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    char cArray[16];
    
    cArray[0] = 'f';  // <--- note a single character is contained within single quotes
    cArray[1] = 'i';
    cArray[2] = 'r';
    cArray[3] = 's';
    cArray[4] = 't';
    cArray[5] = 'h';
    cArray[6] = 'a';
    cArray[7] = 'l';
    cArray[8] = 'f';
    cArray[9] = 's';
    cArray[10] = 't';
    cArray[11] = 'r';
    cArray[12] = 'i';
    cArray[13] = 'n';
    cArray[14] = 'g';
    cArray[15] = '\0';
    
    
    // There are 15 elements in this array, numbered 0 - 15.
    // Some new to this mistakenly try to access "cArray[16]" - ** this is out of bounds! **
        // print entire string at once
    
    printf("%c",cArray[14]);  // <--- %c prints a single character
    
        return 0;
    }
    Last edited by AutoIt Addict; 07-20-2012 at 04:04 PM. Reason: update code

  11. #26
    Registered User
    Join Date
    Jun 2011
    Posts
    2,292
    Write the code to print it to the screen and see!

    That is technically correct, but not the most ideal. See the code directly below the example you copied to see how to declare a string with just one line.

    But before you change even that, I'd recommend printing out that string to the screen to get a feel for it.

  12. #27
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    looks like the scanf function is working
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
    
    char cArray[242];
    
    cArray[0] = 'f';  // <--- note a single character is contained within single quotes
    cArray[1] = 'i';
    cArray[2] = 'r';
    cArray[3] = 's';
    cArray[4] = 't';
    cArray[5] = 'h';
    cArray[6] = 'a';
    cArray[7] = 'l';
    cArray[8] = 'f';
    cArray[9] = 's';
    cArray[10] = 't';
    cArray[11] = 'r';
    cArray[12] = 'i';
    cArray[13] = 'n';
    cArray[14] = 'g';
    
    cArray[27] = 'a';  // <--- note a single character is contained within single quotes
    cArray[28] = 'b';
    cArray[29] = 'c';
    cArray[30] = 'd';
    cArray[31] = 'e';
    cArray[32] = 'f';
    cArray[33] = 'g';
    cArray[34] = 'h';
    cArray[35] = 'i';
    cArray[36] = 'j';
    cArray[37] = 'k';
    cArray[38] = 'l';
    cArray[39] = 'm';
    cArray[40] = 'n';
    cArray[41] = 'o';
    
    cArray[15] = 'p';  // <--- note a single character is contained within single quotes
    cArray[16] = 'q';
    cArray[17] = 'r';
    cArray[18] = 's';
    cArray[19] = 't';
    cArray[20] = 'u';
    cArray[21] = 'v';
    cArray[22] = 'w';
    cArray[23] = 'x';
    cArray[24] = 'y';
    cArray[25] = 'z';
    
    cArray[42] = '\0';
    
    
    // There are 15 elements in this array, numbered 0 - 15.
    // Some new to this mistakenly try to access "cArray[16]" - ** this is out of bounds! **
        // print entire string at once
    
    printf("%c\n",cArray[14]);  // <--- %c prints a single character
    
    // print entire string at once
    
    printf("%s\n",cArray);  // <--- %s prints a null-character-terminated string
    // note that just the array name is given without any brackets/indices
    
    printf("%c\n",cArray[40]);  // <--- %c prints a single character
    
    
    scanf("%s", cArray[40]);
    gets(cArray); //Accept String from User
    
    printf("%s\n",cArray);  // <--- %s prints a null-character-terminated string
    // note that just the array name is given without any brackets/indices
    
    return 0;
    }
    Last edited by AutoIt Addict; 07-20-2012 at 07:01 PM.

  13. #28
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    does system c need to pause the screen before accepting another string?

  14. #29
    Registered User
    Join Date
    Jul 2012
    Posts
    37
    is there any way to create a dill structured library and call on it in c++
    or shall i say
    Last edited by AutoIt Addict; 07-20-2012 at 08:25 PM.

  15. #30
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,673
    Quote Originally Posted by AutoIt Addict View Post
    is there any way to create a dill structured library and call on it in c++
    or shall i say
    1. Make the library
    2. LoadLibrary function
    3. What's the next thing in this 20 questions game?
    rags_to_riches likes this.

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Types
    By Sn0wcra5h in forum C Programming
    Replies: 1
    Last Post: 01-19-2010, 09:33 PM
  2. data types types...
    By gftmc in forum C++ Programming
    Replies: 3
    Last Post: 09-11-2006, 11:30 AM
  3. data types
    By 182 in forum C++ Programming
    Replies: 10
    Last Post: 02-18-2006, 04:24 AM
  4. data types
    By Draco in forum C Programming
    Replies: 7
    Last Post: 06-05-2002, 03:26 AM
  5. data types
    By wazilian in forum C Programming
    Replies: 1
    Last Post: 12-07-2001, 12:52 PM

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