Returning and Printing Strings in C++

1.3k Views Asked by At

I am trying to get a file path from the user in the getPath() function and return the path as a string. I am having trouble because the compiler says i need to use const char's and i dont know how to do that. How would I use const chars and what even are they. Also how do I print them to the console like in the main function.

#include <iostream>
#include <stdio.h>
#include <string.h>

  char getPath() {
     char path[64];
     std::cout << "Input File Name For Debugging:";
     gets(path);
     std::cout << "Debugging: ";
     puts(path);
     return path[64];
 }

int main(){
    char path[64];
    int pathlen = strlen(reinterpret_cast<const char *>(path));
    //suppost to print the char array
    for(int i; i < pathlen; i++){
        std::cout << path[i];
    }
     return 0;
 }
1

There are 1 best solutions below

0
On

Lot's of misunderstandings

1) char is not a string, it's a character

2) An array of chars (e.g. char [64]) is not a string, its an array. It can hold a string but that's a subtly different idea

3) You don't use [64] when you mean the whole array, so return path[64]; is not the correct way to return a string.

4) Don't mix C++ I/O (std::cin, std::cout) with C I/O (puts, gets), it doesn't work reliably, Stick with C++ I/O so

 std::cout << "Debugging: " << path << '\n';

not

 std::cout << "Debugging: ";
 puts(path);

5) You never call your getPath function so of course it doesn't execute

6) You don't initialise your loop variable i in your final loop so it has no predictable value. You should initialise i to 0

for(int i; i < pathlen; i++){
     std::cout << path[i];

should be

for(int i = 0; i < pathlen; i++){
     std::cout << path[i];

As you can see lots and lots of mistakes for a very short program. I'm going to show two different correct ways to write this program.

So there are two ways to represent a string in C++, there's the C++ way and there's the way that C++ inherits from C. The code you are writing above is trying to do things the C way, so I'll show that first, but actually the C++ way is much much easier. I'll show that second, but it's the way you should do things.

The first way is to use an array of characters to hold the string. But arrays have serious problems in C++. In particular it's not possible to return an array from a function, so your code above was never going to work, even if you'd fixed all the smaller problems. The way you get C++ to 'return' an array is a bit curious and I'm not going to explain it properly (you need to read a good C++ book). What you do is declare the array in the calling function and pass the array as a parameter. Here's your program written using this technique (and fixed of all the other problems).

#include <iostream>

void getPath(char path[], int n) {
     std::cout << "Input File Name For Debugging:";
     std::cin.getline(path, n);
     std::cout << "Debugging: " << path << '\n';
}

int main(){
    char path[64];
    getPath(path, 64);
    std::cout << path << '\n';
    return 0;
}

Note I'm using getline to read the string, which is one C++ way to read a string. getline requires that you pass the size of the array it's going to read into, so I've passed that to getPath as well as the array itself.

Now for the easy way. C++ has it's own string type called std::string. You don't need to use tricky arrays at all. And the C++ string type can be returned from a function in the normal way. This makes for much more natural code. To use the C++ string type all you need to do is #include <string>. Here's your program rewritten to use the C++ string type

#include <iostream>
#include <string>

std::string getPath() {
     std::cout << "Input File Name For Debugging:";
     std::string path;
     std::getline(std::cin, path);
     std::cout << "Debugging: " << path << '\n';
     return path;
}

int main(){
    std::string path;
    path = getPath();
    std::cout << path << '\n';
    return 0;
}

Notice this second program is closer to your original code, getPath has a return type, only it's std::string not char, and it has a return statement to return the path. This is the way you should be writing this code, the C++ string type will make writing string code much easier for you.