0

I asked a question about my code a while back and it still doesn't make sense to me. I tried to look at the answers to my old question but still doesnt make sense. I also tried to fix my curly braces--are they wrong?

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

int upper(int input);
int lower(int input);
int main(int argc, string argv[])
{
string k = atoi(argv[1]);
if (argc != 2)
;
int key = argv[1];


    printf("The key must be an int. Please try again.");
    return 1;
    {    
    else   
    }   
    printf("Good, now enter a plaintext!:");
    string plain = GetString();

    for (int i = 0, n = strlen(plain); i < n; i++)
    {
        if (isalpha(plain[i]))
        {
            if (isupper(plain[i]))
            {
                printf("%c", upper(plain[i]));
            }
            if (islower(plain[i]));
            {
                printf("%c", lower(plain[i]));
            }
        }
          else
          {
            printf("%c" , plain[i]);
          }
     }


{
int key = argv[1];

int upper(int input);
int i = input - 65 + key;
int k = i + 65;
return k;


int lower(int input);
int i = input - 97 + key;
i = i % 26;
int k = i + 97;
return k;

}


}

2 Answers 2

1

Looks like you have 3 functions in your program - main, upper, and lower. Each function must follow a format like this:

int Main (void) {

statements

}

Each of your functions must be defined separately with each having all of it's associated statements enclosed within the parent function's curly braces.

Suggest take a closer look at these.

Also, in your if statements, don't end the if line of code with a ; and then place a curly brace. The ; signifies the end of that statement whereas the curly brace is used to group lines of sub-statements linked to your if statements.

Finally, an if statement is a decision point. The above if statements are calling functions but then lack comparisons upon which to make decisions.

0

Unless you correct your syntactic mistakes your code is not going to compile at all, more even run. Let's take your errors one by one.

  1. On line 13 there is a semicolon that shouldn't be there. You are correctly starting your if statement about the argc value but you are not completing in. Keep in mind that an if statement looks like this:

    if (condition)
    {
        // your code here
    }
    
  2. On line 11 you have string k = atoi(argv[1]); but there are 2 mistakes here. First you have to check if the argv[1] exists (the if of the previous statement) and then try to assign it, and second per its man page the atoi function return an int, but you are trying to store the return value at k, which is a string. So your line of thought should probably be something like:

    if (enough arguments)
    {
        assign the atoi of the second argument to k
    }
    else
    {
        yell at the user
    }
    
    get plain text
    
  3. Finally even though your prototypes of your functions are correct, the implementation of these functions are wrong. If your prototype is

    int square(int x);
    

    the function should look like this:

    int square(int x)
    {
        return x * x;
    }
    

Hope these help you with your problem. If you want anything else don't hesitate to ask for help again, and don't be intimidated by those mistakes. I can promise you that in a week you are going to laugh at them.

2
  • What line is one of my prototype functions on?
    – Ani
    Jul 10, 2015 at 14:16
  • Your prototypes are on lines 7 and 8. They are these lines: int upper(int input); and int lower(int input);
    – ChrisG
    Jul 10, 2015 at 15:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .