Help! Cstrings, displaying Cstrings, etc.

Pretty much everything is good to go except when displaying the information using a function. please let me know if there are alternatives to making the code better and cleaner.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
  
#include <cstdlib>

#include<string>
#include <iostream>
void displayInfo(char fullName[], int age);

const int size1= 10;
const int size2=10;
const int size3=20;



using namespace std;

/*
 * 
 */
int main(int argc, char** argv) {
    char firstName[size1]= "tom ";
    char lastName [size2];
    char fullName [size3];
    int age;
    
    cout << "what is your age?" << endl;
    cin >> age;
    cin.ignore();
    
    cout << "what is your last name?"<< endl;
    cin.getline(lastName,size1);
    
    strcpy(fullName,firstName);
    strcat(fullName,lastName);
    
    displayInfo(fullName,age);
    
    
    
    
    
    return 0;
}
void displayInfo(char fullName[], int age)
{
    cout << "hey "<< fullName << "you are: "
         << age << "years old";
}
Last edited on
In my opinion, you don't really want to use C Strings unless you have to.

For this case, you can use C++ strings, and you wont be limited to the length of the name this way.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <cstdlib>

#include<string>
#include <iostream>

using namespace std;

void displayInfo(string fullname, int age);


int main(int argc, char** argv) {
    string firstname;
    string lastname;
    string fullname;
    int age;
    
    cout << "what is your age?" << endl;
    cin >> age;
    cin.ignore();
    
    cout << "what is your first name?"<< endl;
    getline(cin,firstname);
    cin.clear();

    cout << "what is your last name?"<< endl;
    getline(cin,lastname);
    cin.clear();
    
    fullname = firstname + " " + lastname;
    
    displayInfo(fullname,age);
    
    
    
    system("pause");
    
    return 0;
}
void displayInfo(string fullname, int age)
{
    cout << "hey "<< fullname << " you are: "
         << age << " years old";
}
Last edited on
@pindrought oh yeah i know its way easier using c++ strings but unfortunately for the assignment i was given i have to use cstring...smh.
closed account (jvqpDjzh)
In my opinion, you don't really want to use C Strings unless you have to.
I don't understand guys like you who are always reponding in that way! Of course he is learning pointers and arrays and C-strings, and it's very important to know all those things well!
Last edited on
You're constructing the fullname with no space between first and last. So if first="Charlie" and last="Brown" then fullname is "CharlieBrown". To fixthis add strcat(fullname, " "); between lines 32 and 33.

Also, your array sizes seem too small to me. Some names are quite long so I'd make them 50%-100% larger.
Just one comment. if your program uses c-strings rather than c++ std::string, then it should have
 
#include <cstring> // C-strings 
rather than
 
#include <string>  // C++ std::string 
I notice in line 30, you use size1 when you're inputting for lastName. I guess it doesn't matter in this case since size1 and size2 have the same values, but you should still change it.

Also, remember that cstrings have a terminating '/0' character. You have to leave room for that when you're inputting.

In other words, line 30 should be cin.getline(lastName,size2 - 1);.
In other words, line 30 should be

It should in fact be
 
    cin.getline(lastName, size2);
or perhaps clearer,
 
    cin.getline(lastName, sizeof(lastName));


The getline function takes account of the terminating null, you don't need to account for it separately.
http://www.cplusplus.com/reference/istream/istream/getline/
thank you everybody for the advice and help!
I don't understand guys like you who are always reponding in that way! Of course he is learning pointers and arrays and C-strings, and it's very important to know all those things well!


I think you misunderstood what i'm saying.

I agree that it's very important to know how to use C-strings, and understand how pointers/arrays work.


However, I think that in most cases it makes more sense to use strings since you aren't limited to a certain size.

I realize that you can dynamically allocate chars if you don't want to be limited to a size, but it just seems like more work for the same result to me.

However I do agree that it's very important to understand how to utilize char arrays/pointers.
Topic archived. No new replies allowed.