Jump to content

  • Log In with Google      Sign In   
  • Create Account


NUCLEAR RABBIT

Member Since 12 Apr 2006
Offline Last Active May 21 2014 05:42 PM
-----

Topics I've Started

Writing/Reading from files help needed! (C Programming)

21 May 2014 - 05:13 PM

hello, I am trying to write a program that makes 10 empty entries in my file, updating the first 3, and then asking the user which one to erase back to a blank entry, and then reprints the file contents again with the entry deleted. It seems to work okay ONLY if you delete the first entry, if you try to delete the 2nd or 3rd, the output goes all crazy! Any help would be greatly appreciated! :(

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

typedef struct person {
    char first_name[15];
    char last_name[15];
    char age[4];
    int accNum;
} Person;

int main(int argc, char * argv[])
{
    FILE * file = fopen("/Users/MYNAME/Desktop/person.txt", "r+");
    
    Person blankEntry = {"No First Name", "No Last Name", "0", 0};
    Person person1 = blankEntry;
    
    printf("Records:\n");
    for(int i = 1; i <= 10; i++) {
        fwrite(blankEntry.first_name, 1, sizeof(blankEntry.first_name), file);
        fwrite(blankEntry.last_name, 1, sizeof(blankEntry.last_name), file);
        fwrite(blankEntry.age, 1, sizeof(blankEntry.age), file);
        fprintf(file, "%d", i);
        
        printf("%d. %s\t\t\t%10s%10s\n", i, blankEntry.first_name, blankEntry.last_name, blankEntry.age);
    }
    
    printf("\n\nUpdating Record:\n");
    fseek(file, SEEK_SET, 0);
    for(int i = 1; i <= 3; i++) {
        printf("Enter in first name, last name, and age: ");
        scanf("%s %s %s", person1.first_name, person1.last_name, person1.age);
        
        fwrite(person1.first_name, 1, sizeof(person1.first_name), file);
        fwrite(person1.last_name, 1, sizeof(person1.last_name), file);
        fwrite(person1.age, 1, sizeof(person1.age), file);
        fprintf(file, "%d", i);
    }
    
    printf("\n\nRecords:\n");
    printf("First Name:\t\tLast Name:\t\tAge:\n");
    fseek(file, SEEK_SET, 0);
    for(int i = 1; i <= 10; i++) {
        fread(person1.first_name, 1, sizeof(person1.first_name), file);
        fread(person1.last_name, 1, sizeof(person1.last_name), file);
        fread(person1.age, 1, sizeof(person1.age), file);
        fscanf(file, "%d", &person1.accNum);
        
        printf("%i. %s\t\t%10s%10s\n", person1.accNum, person1.first_name, person1.last_name, person1.age);
    }
    
    int deleteKey;
    printf("\n\nEnter in account number to delete: ");
    scanf("%i", &deleteKey);
    
    fseek(file, (deleteKey - 1) * sizeof(struct person), SEEK_SET);
    fwrite(person1.first_name, 1, sizeof(person1.first_name), file);
    fwrite(person1.last_name, 1, sizeof(person1.last_name), file);
    fwrite(person1.age, 1, sizeof(person1.age), file);
    //fwrite(&blankEntry, sizeof(struct person), 1, file);
    
    printf("\n\nRecords:\n");
    printf("First Name:\t\tLast Name:\t\tAge:\n");
    fseek(file, SEEK_SET, 0);
    for(int i = 1; i <= 10; i++) {
        fread(person1.first_name, 1, sizeof(person1.first_name), file);
        fread(person1.last_name, 1, sizeof(person1.last_name), file);
        fread(person1.age, 1, sizeof(person1.age), file);
        fscanf(file, "%d", &person1.accNum);
        
        printf("%i. %s\t\t%10s%10s\n", person1.accNum, person1.first_name, person1.last_name, person1.age);
    }
    
    fclose(file);
    
    return 0;
}

strchr question (C programming)

27 April 2014 - 02:51 PM

Hello, so I was writing a program to print only words that end with "ed" and I thought that the last element in a char array was '\0' so I thought adding \0 to the end of the searchKey would help detect the end of the string but I guess I went about doing this wrong. What would you suggest I look into to accomplish what I'm trying to do?

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(int argc, const char * argv[])
{
    char * lib[] = {"apple", "bat", "create", "destroy", "build"};
    int lib_size = sizeof(lib)/sizeof(lib[0]);
    char * searchKey = "b";
    char * wordPtr[lib_size];
    
    for(int i = 0; i < lib_size; i++)
    {
        wordPtr[i] = lib[i];
    }
    
    size_t searchSize = 1;
    
    printf("Word Bank:\n+ ");
    
    for(int i = 0; i < lib_size; i++)
    {
        printf("%s ", lib[i]);
    }
    
    printf("\n\nWords beginning with 'b':\n+ ");
    
    for(int i = 0; i < lib_size; i++)
    {
        if(strncmp(wordPtr[i], searchKey, searchSize) == 0)
        {
            printf("%s ", wordPtr[i]);
        }
    }
    
    //----------------------------------------------------------------
    
    printf("\n\nNew Word Bank:\n+ ");
    
    char * ed_lib[] = {"walke", "ededededs", "paniced", "play", "music", "education"};
    int ed_size = sizeof(ed_lib)/sizeof(ed_lib[0]);
    char * ed_searchKey = "ed\0";
    
    for(int i = 0; i < ed_size; i++)
    {
        printf("%s ", ed_lib[i]);
    }
    
    printf("\n\nWords ending in 'ed'\n+ ");
    
    char * edPtr = ed_lib;
    
    for(int j = 0; j < ed_size; j++)
    {
        edPtr = ed_lib[j];
        
        if(strstr(&edPtr[j], ed_searchKey) != NULL)
        {
            printf("%s ", ed_lib[j]);
        }
    }
    
    puts("");
    
    return 0;
}

Array Location? (C Programming)

23 April 2014 - 01:22 PM

Hello, I am writing a program that takes a sentence and tranforms that sentence into the morse code equivalent. Problem is, when I print the text after the convertion, all the letters after the 1st letter are the same morse code symbol, instead of the correct letter equivalent :/ I been looking over my code trying to see why this is happening, but I cannot see what the problem can be! Can anyone please help me see what I am doing wrong? I'd surely appreciate any help! ohmy.png

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define LETTERS 26
#define NUMBERS 10

void convertToLower(char * sPtr);

int main(int argc, const char * argv[])
{
    // Morse Code 0-9
    //char * num_codes[] = {"-----", "----.", "---..", "--...", "-....", ".....", "....-", "...--", "..---", ".----"};
    // Morse Code A-Z
    char * alp_codes[] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
    // A-Z
    const char searchKey[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
    
    // string variables & attributes
    char random_text[] = "abcd";
    char * txtPtr = random_text;
    int text_size = sizeof(random_text)/sizeof(random_text[0]);
    char * morse_code[text_size];
    int alp_index;
    
    convertToLower(random_text);
    
    // seaches for the searchKey in the txt and
    // converts that character of random_txt to the alp_code
    for(int h = 0; h < text_size; h++)
    {
        for(int i = 0; i < LETTERS; i++)
        {
            txtPtr = strchr(txtPtr, searchKey[i]);
            alp_index = (int)(txtPtr - random_text);
            
            if(txtPtr != NULL)
            {
                txtPtr++;
                morse_code[h] = alp_codes[alp_index];
                break;
            }
        
            txtPtr = random_text;
        }
    }
    
    // prints morse code
    for(int i = 0; i < text_size-1; i++)
    {
        printf("%s | ", morse_code[i]);
    }
    
    return 0;
}

//----------------------------------------------------------------------------------------
//  FUNCTION DEFINITIONS

void convertToLower(char *sPtr)
{
    while(*sPtr != '\0')
    {
        *sPtr = tolower(*sPtr);
        *sPtr++;
    }
}

String help in C

16 April 2014 - 09:25 PM

Hello, I am trying to write a program that counts how many times a letter occurs in a string. I know there's easy ways to do it, but I want to use the strchr function to have a search key. I wrote this and for some reason, nothing even happens when I run it! No errors or nothing so I'm not sure what the problem could be! Any help is appreciated sad.png

#include <stdio.h>
#include <string.h>

int main(int argc, const char * argv[])
{
    const char * random_text = "Once there was this really old man who tranformed into dust. The end!";
    
    int counter = 0;
    int searchKey = 'd';
    char * strPtr = strchr(random_text, searchKey);
    
    do {
        if(strPtr != NULL)
           counter++;
        
        strPtr = strchr(random_text+1, searchKey);
    } while (strPtr != NULL);
    
    printf("%s\n%s\n\n%s%c%s%d%s", "The text:", random_text, "The occurence of ",
           searchKey, " happened ", counter, " times.\n");
    
    return 0;
}

Array Problem in C

30 March 2014 - 07:43 PM

Hello, so I am trying to write a program that creates an array with random numbers and then making a new array with the same values but with no duplicates. When I run the program, the results are not what they should come out being and I'm not too sure why this his happening! ohmy.png If anyone could shed some light on my brain it would be greatly appreciated!

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void binarySearch(int rand_nums[], int size);
void printArray(int nums[], int size);

int main(int argc, const char * argv[])
{
    srand((unsigned)time(NULL));
    
    int rand_nums[20];
    int rand_num_size = sizeof(rand_nums)/sizeof(rand_nums[0]);
    
    // fills array with random nums 0-10
    for(int i = 0; i < rand_num_size; i++)
        rand_nums[i] = rand() % 10;
    
    binarySearch(rand_nums, rand_num_size);
    
    return 0;
}

void printArray(int nums[], int size) {
    printf("Array set: ");
    for(int i = 0; i < size; i++)
        printf("%d ", nums[i]);
}

void binarySearch(int rand_nums[], int size) {
    int counter = 0;
    
    // goes through array and makes duplicates NULL
    for(int i = 0; i < size; i++) {
        for(int j = i + 1; j < size; j++) {
            if(rand_nums[i] == rand_nums[j] && rand_nums[j] != (int)NULL && rand_nums[i] != (int)NULL) {
                rand_nums[j] = (int)NULL;
                counter++;
            }
        }
    }
    
    // new array with custome size
    int new_array[size-counter];
    size_t new_array_size = sizeof(new_array)/sizeof(new_array[0]);
    
    int index = 0;
    
    // fills new array with non NULL values of rand_nums
    for(int g = 0; g < new_array_size; g++) {
        if(rand_nums[g] != (int)NULL) {
            new_array[index] = rand_nums[g];
            index++;
        }
    }
    
    // victory
    printf("OLD Array:\n");
    printArray(rand_nums, size);
    printf("\n\nNEW Array:\n");
    printArray(new_array, new_array_size);
}


PARTNERS