samuraitux's Profile User Rating: -----

Reputation: 9 Worker
Group:
Contributors
Active Posts:
65 (0.03 per day)
Joined:
29-April 09
Profile Views:
5,228
Last Active:
User is offline Sep 02 2013 11:58 AM
Currently:
Offline

Previous Fields

Country:
US
OS Preference:
Linux
Favorite Browser:
Who Cares
Favorite Processor:
AMD
Favorite Gaming Platform:
Who Cares
Your Car:
Who Cares
Dream Kudos:
25

Latest Visitors

Icon   samuraitux has not set their status

Posts I've Made

  1. In Topic: Weird output from program.

    Posted 2 Sep 2013

    View Postjimblumberg, on 02 September 2013 - 01:14 PM, said:

    Quote

    Thanks for the warning. would I need to subtract maybe sizeof(array) - strlen(array) to get space left or is there a better way for me to do this check?

    Check the strlen() of both strings, if you have enough space in your msg[] array to hold these added characters then do the strcat().

    Since this is a single function program you can use sizeof() to determine the size of your array, but when you start using functions sizeof only works where the array was defined. I recommend you use a constant for your array sizes.

    // Before main
    #define MAX_ARRAY 999;
    
    ...
    
    char myArray[MAX_ARRAY];
    
    


    Quote

    The reason I removed the end of line character was because it was causing issues with my checks for just a period on a line by itself.

    To me it would be easier to just add the new line character to your constant:

            if(strcmp(line, ".\n") == 0)
    
    



    Jim

    Thanks Jim. I have attached my new code. Thanks for your explanations, they are really helpful.

    Also vividexstance I have bookmarked the site and I read through the strcat(). One thing I just learned from that is that it will actually overwrite the null terminator zero but as Jim pointed out it won't do any bounds checking.

    Again this is all really good information.

    One more question that I thought of. Is it best practice that if I am going to put a hard coded value like for my array that I should look at making a Constant so that if I choose to extend it in the future I can just use modify the constant and do easier value checks against it?



    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define MAX_ARRAY 999
    
    int i;
    int main()
    {
        char line[MAX_ARRAY] = {0}; /* Holds User Input */
        char msg[MAX_ARRAY] = {0}; /* Holds users message. */
    
        printf("Enter your message. Enter a . on a line by itself to end.\n");
        while(1)
        {
            fgets(line, sizeof(line), stdin);
            if(strcmp(line, ".\n") == 0)
            {
                break;
            }
    
            /* Move to a function after learning how to create my own functions. */
            /* Which will be in 10 more chapters. */
            /* Added the + 1 to take into account the string terminator. */ 
            if((strlen(line) + strlen(msg) + 1) <= MAX_ARRAY)
            {
                strcat(msg, line);
            }
        }
        printf("\nYour message %s will be sent.\n", msg);
        return 0;
    }
    
    
  2. In Topic: Weird output from program.

    Posted 2 Sep 2013

    View Postjimblumberg, on 02 September 2013 - 12:59 PM, said:

    Be careful with that strcat(). The way your program is structured you could quite easily access your msg[] array out of bounds. You need to make sure you have room left in your array to add the other array before you blindly strcat() these two arrays.

    Also why are you removing the end of line character on line 16 then overwriting the end of string character with this character on line 22? If your C-string had not been initialized to all zeros you would be in trouble.

    Jim

    Thanks for the warning. would I need to subtract maybe sizeof(array) - strlen(array) to get space left or is there a better way for me to do this check?
    The reason I removed the end of line character was because it was causing issues with my checks for just a period on a line by itself. But after the check I wanted the formating to be there so that when I truely get it to send out as a message, the message is not on a single line.

    I am sure there are several cools ways to get around this but with my limited knowledge (Basically I have been reading on C for only 3 days straight now) I couldn't come up with any. But if you have any suggestions I am ready to learn them and try them out.

    Thanks
  3. In Topic: counting int array entities that are the same

    Posted 2 Sep 2013

    View Postwjg284, on 02 September 2013 - 12:52 PM, said:

    In the display function, I seem to get a lot of garbage returned to me along with the expected output
    #include<iostream>
    using namespace std;
    
    void ages(int[], int, int, int);
    void display(int[], int);
    
    
    //************************************************************************
    //                        MAIN                                          //
    //************************************************************************
    
    int main(){
        
        int size=50;
        int getAge[size];
        int age=0;
        int count=0;
        
        cout<<"Input an age, 0 to 100, -99 to stop. \n";
        ages(getAge,size,count,age);
        display(getAge,size);
        
    system("pause");
    return 0;
    }
    
    //************************************************************************
    //                   AGES                                               //
    //************************************************************************
    
    void ages(int getAge[], int size, int count, int age){
    
         cin>>age;
         
         while(age != -99 && count < size){
              count++;
              getAge[count-1]=age;
              cout<<"Input an age, 0 to 100, -99 to stop. \n";
              cin>>age;
              }
         }
              
    //************************************************************************
    //                   DISPLAY                                            //
    //************************************************************************                
         
    void display(int getAge[],int size){
         
    
         int temp = 0;
         for(int count=0; count < size ; count++){
                 
              if(getAge[count] > 0){
                   temp=0;  
                   for(int index=0; index<size; index++){
                        if(count+1 == getAge[index]) 
                             temp+=1;
                   }
              if(temp != 0)
              cout<<"\nThe total number of people " << count + 1 << " years is old " << temp <<endl;
              }   
         }
    }
    
    


    This sounds exactly like what was happening to me. What fixed this for me was initializing all of my Arrays. Try this and see if it helps.
     int getAge[size] = {0};
    
    


    One of the senior memebers helped me with a problem a few minutes ago and that was what helped me.
  4. In Topic: Weird output from program.

    Posted 2 Sep 2013

    Thanks everyone for your help.

    I made some few tweaks to the final program. I am posting just in case someone is looking for something similar.

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int i;
    int main()
    {
        char line[999] = {0}; /* Holds User Input and Initializes array */
        char msg[999] = {0}; /* Holds users message and Initializes array */
        printf("Enter your message. Enter a . on a line by itself to end.\n");
        while(1)
        {
            fgets(line, sizeof(line), stdin);
            /* This will remove the new line character. and allows me to test for a single period on the screen. */
            /* Look at changing in the future to be just an empty line. */
            line[strlen(line) - 1] = '\0';
            if(strcmp(line, ".") == 0)
            {
                break;
            }
            /* Add newline back in so that when sending as an email it will format correctly. */
            line[strlen(line)] = '\n';
            strcat(msg, line);
        }
        printf("\nYour message %s will be sent.\n", msg);
        return 0;
    }
    
    
    
  5. In Topic: Weird output from program.

    Posted 2 Sep 2013

    View Postvividexstance, on 02 September 2013 - 12:17 PM, said:

    I didn't change a thing and this is the result I get from running your code:

    Quote

    ./strcatTest
    Enter your message. Enter a . on a line by itself to end.
    hello world
    .

    Your message hello world will be sent.


    View Postsamuraitux, on 02 September 2013 - 01:13 PM, said:

    Thanks CTphpnwb, That did the trick. Could I trouble you to explain why not initializing the array caused the crazy output? Also should I always initialize an array to keep this from happening? Thanks.

    Here's a neat little trick that clears the array to all zeroes:
    char msg[999] = { 0 };
    
    



    Thanks. I forgot to clear that note once I got the msg output to work for me.

My Information

Member Title:
D.I.C Head
Age:
31 years old
Birthday:
February 14, 1983
Gender:
Programming Languages:
C, Python, C++, VBScript, Shell Scripting, Powershell

Contact Information

E-mail:
Private
Website URL:
Website URL  http://www.samuraitux.com

Friends

samuraitux hasn't added any friends yet.

Comments

samuraitux has no profile comments yet. Why not say hello?