0

I'm in my first CS class and I am having trouble getting this to compile. I have looked over my professor's notes, lectures, and examples but it doesn't seem to matter what I type, its an error. I'm getting 38 errors and there's only 25 lines of code! A lot of the errors don't make any sense, for example, "expected a ;" even though there is already a ';', Or "expected a {" after main even though it is clearly there. As far as I can see, visual studio should at least compile my code. Any help at all is much appreciated!

Step by Step Instructions/Correct Output:

Write a program that produces the following output:

/* OUTPUT

Enter your age: 21

Enter the last name: Lee

Hello Tom Lee. You are 21 years old.

Press any key */

1.) Declare an array named: firstName

  • The array is a c_string, i.e., it is a null-terminated character array.

  • The size of the array is 10.

  • Assign a first name to it when it is declared.

2.) Declare an array named: lastName

  • The array is a c_string,

  • The size of the array is 10.

  • Don’t assign a name to it.

3.) Declare an array named: fullName

  • The array is a c_string,

  • The size of the array is 20.

  • Don’t assign a name to it.

4.) In main():

  • First ask the user for the age.

  • Read the age and assign it to a variable.

  • Then ask the user for the last name.

  • Read the last name and assign it to lastName.

  • Assign the first and last names to fullName.

  • Make sure to include a space between the names.

5.) Call a function named: displayInfo().

  • The function should output the full name and age. (see output)

My code:

#include <iostream>
#include <string>
using namespace std;

void displayInfo(char fullName, int age)

int main
{
    char firstName[10] = "Bob";
    char lastName[10] = { 0 };
    char fullName[20] = { 0 };
    int age;
    cout << "Enter your age:  ";
    cin >> age;
    cout << "\nEnter the last name:  ";
    cin.getline(lastName, 10);

    displayInfo(fullName, age)

    strcpy_s(fullName, firstName);
    strcat_s(fullName, " ");
    strcat_s(fullName, lastName);
    strcat_s(fullName, ".");

    return 0;
}
displayInfo(char fullName, int age)
{
    cout << "Hello " << fullName << "You are " << age << "years old.";
}
dribs
  • 17
  • 1
  • 5
  • 1) `main()` is a function (note the parenthses). 2) Using non-standard C++ such as `strcpy_s` isn't a good start in learning C++. 3) Get an actual C++ person to teach the language, as there are several issues with your code. – PaulMcKenzie Nov 28 '17 at 20:20
  • This `void displayInfo(char fullName, int age)` is where the `;` is missing, just before `main`. – Bo Persson Nov 28 '17 at 20:27
  • And when mixing `>>` with `getline` you want to first read the favorite [Why does std::getline() skip input after a formatted input?](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction) – Bo Persson Nov 28 '17 at 20:32
  • @Paul Unfortunately, the `_s` functions are an optional part of the standard. –  Nov 28 '17 at 20:35

1 Answers1

1

You have a few bugs with your code, here are some corrections (in comments):

#include <iostream>
#include <fstream>
#include <cstring> // include this for strcpy() and strcat()

using namespace std;

// You need to have char* fullname as you are passing a cstring not a character
void displayInfo(char* fullName, int age); // You forgot the semi colon here

int main(void) // You need to have input parameters to main() it can be void
{
    char firstName[10] = "Bob";
    char lastName[10] = "0"; // Not an error but you should initialize like this
    char fullName[20] = "0"; // same here
    int age;
    cout << "Enter your age:  ";
    cin >> age;
    cout << "\nEnter the last name:  ";
    cin.getline(lastName, 10);

    strcpy(fullName, firstName); // Just use strcpy
    strcat(fullName, " "); // Just use strcat
    strcat(fullName, lastName); // Just use strcat
    strcat(fullName, "."); // Just use strcat

    // Move the display after you do string manipulation
    displayInfo(fullName, age); // You forgot semi colon here

    return 0;
}

// You need to have char* fullname as you are passing a cstring not a character
void displayInfo(char* fullName, int age) // You forgot the return type of this function
{
    cout << "Hello " << fullName << "You are " << age << "years old.";
}
SegFault
  • 2,308
  • 3
  • 18
  • 38
  • You should initialize char arrays as `char lastName[10] = "";` Maybe you meant to use `\0`? – Cris Luengo Nov 28 '17 at 21:01
  • It might also be a good idea to teach the use of `std::string` instead of `char*`. It would significantly simplify the code. – Cris Luengo Nov 28 '17 at 21:03
  • @PhotometricStereo Thanks so much, really appreciate it, I like how you commented the corrections instead of doing it for me so I can learn where I made my mistakes. Code is working now. :D – dribs Nov 28 '17 at 21:49