0

I am trying to find the lenght of a string using size_t in my program, then using that integer to determine how many times my program loops to perform a letter shuffle (+6 to a letter so a = g when saved).

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

int main(void)
{
    FILE *file;
    int i, fnlen, lnlen;
    char firstname[15], lastname[15],  *ptr;

    fflush(stdin);
    printf("Please enter the first name of the player:");
    if(fgets(firstname, sizeof(firstname), stdin) != NULL)
    {
        size_t fnlen = strlen(firstname);
    } 
    printf("Please enter the last name of the player:");
    if(fgets(lastname, sizeof(lastname), stdin) != NULL)
    {
        size_t lnlen = strlen(lastname);
    }

    for(i = 0; i < lnlen; i++)
    {
        lastname[i] = (char)((lastname[i] - 'a' + 4) % 26 + 'a');    
    }
    for(i = 0; i < fnlen; i++)
    {
        firstname[i] = (char)((firstname[i] - 'a' + 4) % 26 + 'a');        
    }

    file = fopen("text.txt", "wt");
    if(!file)
    {
        printf("File is not able to open.");
        exit(1);
    }
    fprintf(file, "Firstname is : %s\n""Lastname is: %s\n", firstname, lastname)

When I open the file its saved to, the first name is shuffled properly, but has 8 characters when i typed 5, and the lastname is correctly saved, but the letters are no shuffled, they have just output what I input in the fgets

Thecube
  • 71
  • 8
  • What names did you typed and what result did you get? – fdermishin Dec 09 '15 at 12:06
  • Are you sure that saving to the file works as you expect? – fdermishin Dec 09 '15 at 12:08
  • Saving the file works as expected, as I did that part before I did this and it is all saving fine – Thecube Dec 09 '15 at 12:11
  • Try to remove two loops, where you modify `lastname` and `firstname`. Then you will see the strings before shuffling, so you will have an idea, why you get 8 characters instead of 5. – fdermishin Dec 09 '15 at 12:25
  • Also, look at values of len and fnlen before the loops. – fdermishin Dec 09 '15 at 12:26
  • Hmm the number for firstname lenght is 8 when it should be 5 after `printf("%d", fnlen);`, the second lenght is coming out to a 10 digit numbers after `printf("%d", len);` and i did a printf of both first and last name and they are correct. – Thecube Dec 09 '15 at 12:33
  • Take a look at this question: http://stackoverflow.com/questions/2524611/how-to-print-size-t-variable-portably – fdermishin Dec 09 '15 at 12:39
  • If i have test as the first name and testing as the second name, on the file output it is firstname is : xiwx\RRI Lastname is: testing. Seems that regardless of the lenght of the name written in for firstname it is always coming out to 8, and the letter shuffle is doing nothing for the lastname – Thecube Dec 09 '15 at 12:43
  • 1
    Please post a complete minimal example reproducing the issue. We are missing the way you output the values and the declaration of `lastname` and `firstname`. – Richard St-Cyr Dec 09 '15 at 12:45
  • Have edited the code i provided – Thecube Dec 09 '15 at 12:57
  • Fixed the problem, i was redeclaring len and fnlen. – Thecube Dec 09 '15 at 13:05

1 Answers1

1

The major issue I see with you code is that you are declaring the size_t length variables inside of the if statements. This way any changes you make to them will not be reflected outside of the if statements. I am surprised your code compiles given this (unless you also declared another size_t len and fnlen above what you are showing us in which case by redeclaring it inside of the if statement you are shadowing it.) I would rewrite the code like this. I think you probably had them declared above and initialized them to zero. In this case you you declared a completely different variable (with the same name) inside of the if statements it had not effect and the length variables used in the loop still had zero value.

printf("Please enter the first name of the player:");
size_t fnlen = 0; //I declare them once outside of the if
size_t len = 0;
if(fgets(firstname, sizeof(firstname), stdin) != NULL)   
{
    fnlen = strlen(firstname); //do not redeclare them inside just assign
} 
printf("Please enter the last name of the player:");
if(fgets(lastname, sizeof(lastname), stdin) != NULL)
{
    len = strlen(lastname);
}
ptr = lastname;
while( *ptr != '\n') ++ptr;    
*ptr = '\0';
for(i = 0; i < len; i++)
{
    lastname[i] = (char)((lastname[i] - 'a' + 6) % 26 + 'a');    
}
for(i = 0; i < fnlen; i++)
{
    firstname[i] = (char)((firstname[i] - 'a' + 6) % 26 + 'a');        
}
chasep255
  • 11,745
  • 8
  • 58
  • 115
  • I declare them at the top as just an integer, i then use the if statement to assign them the number which is the string lenght – Thecube Dec 09 '15 at 12:51
  • Yes but inside the if do not declare them as size_t. Adding the size_t REDECLARES them as a different variable. Just say len=strlen(...) – chasep255 Dec 09 '15 at 12:52
  • Thank you! That solved most of the issues. As it is now(after your help) when saved the string is doing 1 extra on each, so if i type in test it will be 5 instead of for, should I just -1 from the len and fnlen? – Thecube Dec 09 '15 at 13:00
  • Measure the length after you replace the new line with null because the newline is being counted. – chasep255 Dec 09 '15 at 13:06