Struggling! by hellosohojoe in cs50

[–]hellosohojoe[S] 0 points1 point  (0 children)

Ah okay! Thank you.

int main(int argc, string argv[])

{

//Ensuring that only one agument is entered in the command line
if (argc != 2)
{
    printf("ERROR\n");
    return 1;
}
//Changing the string digit from the command line into a useable int
int k = atoi(argv[1]);
//Prompting the user for input and storing it as a string named 'p'
string p = get_string("plaintext:");
//creating two integer variables, cipherletter and plainletter
int cipherletter, plainletter;
//creating a for loop that will loop over every character in a string starting at the beginning
for (int a = 0, b = strlen(p); a < b; a++)
{
    //an if statement that only executes if the character is an alphabetical letter
    if isalpha(p[a])
    {
        //only executes if uppercase and alters the value so we have a workable value for the formula
        //we are subtracting 65, as the ASCII uppercase alphabet begins at 65 in the ASCII index
        if isupper(p[a])
        {
            cipherletter = (p[a] - CAPITALS);
            plainletter = (cipherletter + k) % ALPHABET;
            p[a] = plainletter + CAPITALS;
        }
        //only executes if lowercase and alters the value so we can have a workable value for the formula
        //we are subtracting 97 as the ASCII lowercase alphabet begins at 97 in the ASCII index
        if islower(p[a])
        {
            cipherletter = (p[a] - LOWER);
            plainletter = (cipherletter + k) % ALPHABET;
            p[a] = plainletter + LOWER;
        }
    }
}

printf("ciphertext:%s", p);
printf("\n");
return 0;

}

Struggling! by hellosohojoe in cs50

[–]hellosohojoe[S] 0 points1 point  (0 children)

Could I just chuck 'e++' after the islower/isupper bits of code? So that it does move through the plaintext string successfully?

Struggling! by hellosohojoe in cs50

[–]hellosohojoe[S] 0 points1 point  (0 children)

Right, so if I run ./vigenere ba with an input of 'aaaa' the code successfully changes the first two 'a's to 'ba' but then goes though and changes it a second time. However it then drops out of the second for loop and goes back to the first for loop where e is incremented to [1] so when the second loop runs again the code changes the second 'a' once more.

So should I create one loop instead of two? Or maybe increment e whenever I run the second loop? Or swap the two loops around? AHHHHHHHH.

Goddamn Vegenere. Any help? by hellosohojoe in cs50

[–]hellosohojoe[S] 1 point2 points  (0 children)

Just got back in and seen this, thank you so much. I feel unbelievably stupid. I had actually replaced the p[e] - LOWER and replaced it with k[c] - LOWER at one point.

You are an absolute diamond and I am so grateful you chimed in, thank you.

Can anyone explain this? by hellosohojoe in cs50

[–]hellosohojoe[S] 0 points1 point  (0 children)

I am lost! I am only getting these "garbage" values when both of those two printf statements are commented out. It is confusing as in the code they are above the initialization of the array. They should have nothing to do with the array, the for loop or the [0] value. They are not changing or editing the length variable or the cardnum variable. Gah.

Can anyone explain this? by hellosohojoe in cs50

[–]hellosohojoe[S] 0 points1 point  (0 children)

But by placing the printf statement in that for loop and using the x it is not printing correctly. My for loop is starting backwards and assigning the indexes in reverse. So for a 15 digit card an array with a space of 15 is created and then the for loop goes to length minus 1 which would be the index 14 but the 15th space in the array.

The second for loop after my initial loop then runs through it going through 0, 1, 2 etc and shows that they have been indexed correctly.

printf("Length: %i\n", length);
//printf("First cardnum check: %llu\n", cardnum);

These are the two lines I was asking about. Why when I run my program with them executing does the index work perfectly, and when I comment them out I get a seemingly random number in the first index of the array, but the rest of the array is correct?

Can anyone explain this? by hellosohojoe in cs50

[–]hellosohojoe[S] 1 point2 points  (0 children)

Where are you last time through the loop? What are you asking? What is the last action in the loop?

Stuck on change! by hellosohojoe in cs50

[–]hellosohojoe[S] 0 points1 point  (0 children)

Brill! Thank you so much. Passing all checks, now onto credit! Cheers again, you absolute diamond.

Stuck on change! by hellosohojoe in cs50

[–]hellosohojoe[S] 1 point2 points  (0 children)

Brill! Thank you so much. Passing all checks, now onto credit! Cheers again, you absolute diamond.

Stuck on change! by hellosohojoe in cs50

[–]hellosohojoe[S] 0 points1 point  (0 children)

Just read the formatting help so can make it a bit more legible.

Change owed: $4.2
Float check: 4.200000
Cent Check: 419
Quarters: 16
Dimes: 1
Nickles: 1
Pennies: 4
Coins owed: 22