1

My c++ program is this:

#include <iostream>
#include <cstdlib>
#include <dos.h>
using namespace std;

void arr(int pts,string *splt_msg){
  for(int x=-1;x<pts-1;x++){
    string input;
    cout<<"Enter part "<<x<<endl;
    cin>>input;
    (*splt_msg)[x]=input;
  }
}

void print_arr(string *splt_msg,int del,int parts){
  for(int x=-1;x<parts-1;x++){
    cout<<(*splt_msg)[x];
    delay(del);
  }
}

int main(){
  cout<<"Parts in message: ";
  int parts;
  cin>>parts;
  cout<<"Delay between parts(ms): ";
  int del;
  cin>>del;
  parts--;
  string splt_msg[parts];
  string (*Parr)[parts]=&splt_msg;
  arr(parts,Parr);
  print_arr(Parr,del,parts);
  return 0;
}

Here is the error code(on windows and mingw):

c:\Users\User\Desktop\c++>g++ -o program test1.cpp
In file included from test1.cpp:3:0:
c:\mingw\include\dos.h:54:2: warning: #warning "<dos.h> is obsolete; consider using <direct.h> instead." [-Wcpp]
  ^~~~~~~
test1.cpp: In function 'void arr(int, std::__cxx11::string*)':
test1.cpp:18:14: error: 'delay' was not declared in this scope
delay(del);
        ^
test1.cpp: In function 'int main()':
test1.cpp:32:17: error: cannot convert 'std::__cxx11::string (*)[parts] {aka std::__cxx11::basic_string<char> (*)[parts]}' to 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' for argument '2' to 'void arr(int, std::__cxx11::string*)'
   arr(parts,Parr);
                 ^
test1.cpp:33:27: error: cannot convert 'std::__cxx11::string (*)[parts] {aka std::__cxx11::basic_string<char> (*)[parts]}' to 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' for argument '1' to 'void print_arr(std::__cxx11::string*, int, int)'
   print_arr(Parr,del,parts);
                           ^

Does someone know what i have done wrong and the solution? Thanks for the help, I'm just trying to write a simple program to test some concepts out like passing pointers into functions but it doesn't seem to be working.

  • what is this line supposed to mean `string (*Parr)[parts]=&splt_msg;` ? Seriously, I have no clue – 463035818_is_not_an_ai Oct 28 '19 at 20:19
  • 1
    @formerlyknownas_463035818 That is the declaration for an VLA of pointers to `std::string`'s called `Parr`. Definitely not what they want. – NathanOliver Oct 28 '19 at 20:20
  • Heed the warning about dos.h being obsolete. The `delay` function vanished decades ago. Look into [`std::this_thread:::sleep_for`](https://en.cppreference.com/w/cpp/thread/sleep_for) for a modern replacement. – user4581301 Oct 28 '19 at 20:20
  • Where did you define or include the definition of `string` from? – Thomas Matthews Oct 28 '19 at 20:20
  • if you want to learn pointers you better use an example that actually requires pointers. There is no need to use them in your code. Though, from the top of my head I dont know a good example for something that needs a raw pointer, I don't remember when I used one last time... there are really more important things to learn – 463035818_is_not_an_ai Oct 28 '19 at 20:22
  • Note that [anything along the lines of `std::string someArray[parts]` is ill-formed](https://stackoverflow.com/q/57367473/10957435). –  Oct 28 '19 at 20:27
  • Why do you want to pass pointers to functions? Are you sure you want C++ and not C? – JHBonarius Oct 28 '19 at 20:59

2 Answers2

1

This code is more C style then C++. You are doing difficult error prone things. Try using vectors and such

string splt_msg[parts];

should not even evaluate, as parts is not a constant expression.

Try using things like std::vector. and passing by reference. E.g.:

#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>

template<typename T>
T Read() noexcept {
    T output{};
    std::cin >> output;
    return output;
}

void arr(std::vector<std::string>& splt_msg) {
    for (auto& msg : splt_msg) {
        std::cout << "Enter part \n";
        std::cin >> msg;
    }
}

void print_arr(std::vector<std::string> const& splt_msg, int delay) {
    for (auto& msg : splt_msg) {
        std::cout << msg;
        Sleep(delay);
    }
}

int main() {
    std::cout << "Parts in message: ";
    auto parts = Read<int>();

    std::cout << "Delay between parts(ms): ";
    auto delay = Read<int>();

    std::vector<std::string> splt_msg(parts);
    arr(splt_msg);
    print_arr(splt_msg, delay);
}

edit: or even better: use an object

#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>

template<typename T>
T Read() noexcept {
    T output{};
    std::cin >> output;
    return output;
}

class SplitMessage {
private:
    std::vector<std::string> splitMessage;
public:
    SplitMessage(size_t length) noexcept : splitMessage(length) {}

    void GetData() noexcept;
    void PrintData(int delay) const noexcept;
};

void SplitMessage::GetData() noexcept {
    for (auto& msg : splitMessage) {
        std::cout << "Enter part \n";
        std::cin >> msg;
    }
}

void SplitMessage::PrintData(int delay) const noexcept {
    for (auto& msg : splitMessage) {
        std::cout << msg;
        Sleep(delay);
    }
}

int main() {
    std::cout << "Parts in message: ";
    auto parts = Read<int>();

    std::cout << "Delay between parts(ms): ";
    auto delay = Read<int>();

    SplitMessage splt_msg(parts);
    splt_msg.GetData();
    splt_msg.PrintData(delay);
}
JHBonarius
  • 10,824
  • 3
  • 22
  • 41
0

Both functions require a std::string* (a pointer to a string) as first parameter, but both times you pass std::string*[] (an array of pointers to a string) as first paramater. The solution depends on what you want to achieve, but I guess, you want to call the function for one element of the array with an indexer: print_arr(Parr[0],del,parts);

Some other notes:

  • for(int x=-1;x<parts-1;x++){cout<<(*splt_msg)[x];...} will access the -1st character of a string, which is UB, but probably just crashes. Should be for(int x=0;x<parts;x++)
  • string splt_msg[parts]; is no valid C++, since parts is not constant at compile time.
Lukas-T
  • 11,133
  • 3
  • 20
  • 30