-1

The original code where this error occurred is a tool I made in an online C++ compiler but I've simplified the code so its easier to understand

What this code is supposed to do is you input an integer indicating the amount of sentences you want to connect. Then you enter each sentence and when you entered the amount of sentences you specified then you get the all the sentences in a single string and end the program with an exit code of 0. However when you increase the size of the stringInput array(by changing the InputSize definition to something like 600) the result you get at the end is a bunch of wrong characters and symbols that have nothing to do with what you inputted:


#include <iostream>
#include <bits/stdc++.h>
#include <string>
using namespace std;

string convertToString(char* a, int size) //character array to string function
{
    int i;
    string s = "";
    for (i = 0; i < size; i++) {
        s = s + a[i];
    }
    return s;
}
#define InputSize 300 // This is the value that bugs out whenever its larger than 400 sometimes when its on 350 it randomly works or bugs out

int main()
{
    int AmountOfStrings = 1;
    char StringInput[InputSize];
    char CleanChar;
    
    string FinalString = "";
    
    cout<<"amount of connected sentences:";
    
    cin>> AmountOfStrings;
    
    for (int i=-1; i < AmountOfStrings; i++){
    if (i != -1){
    cout<<"sentence n";
    cout<<i + 1;
    cout<<":\n";
    }
    cin.getline(StringInput,InputSize); // get the character array of what the user inputs
    
    FinalString = FinalString + convertToString(StringInput,InputSize); //convert the char array to string and connect the final string to the inputed sentence
    for(int i = 0;i < InputSize;i++){
        
        StringInput[i] = CleanChar; 
        
    }// this loop is to iterate trough all StringInput characters and set them to nothing 
    }
    cout<<FinalString;
    
    return 0;
}
  • 5
    You may be interested in reading [Why should I not #include ?](https://stackoverflow.com/q/31816095/11082165) and [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/11082165) – Brian61354270 Mar 01 '23 at 23:19
  • 1
    There's no good reason to be using macros for constants and C-style arrays in ordinary C++. You will save yourself many tears by making `InputSize` a `constexpr` variable and `StringInput` a `std::array` or `std::vector` – Brian61354270 Mar 01 '23 at 23:22
  • What is the point of `convertToString`? `FinalString + StringInput` should be sufficient... You should just use `std::string` instead of `char[]` – ChrisMM Mar 01 '23 at 23:22
  • Can you please fix your indentation and horizontal whitespace to make this easier to read? – JohnFilleau Mar 01 '23 at 23:23
  • Potential bug: you never check whether `cin>> AmountOfStrings` succeeds, nor any of the calls to `cin.getline` – Brian61354270 Mar 01 '23 at 23:23
  • 2
    `CleanChar` is uninitialized. – Retired Ninja Mar 01 '23 at 23:23
  • 1
    Your `convertToString` puts all 300 characters in the string. A string can be created directly from a `char*` so your conversion function is just messing things up. – Ted Lyngmo Mar 01 '23 at 23:24

1 Answers1

0

Your convert function converts all entries from the original character array. That data is "uninitialized" and can have any character value, including normally non-displayable ones.

I have a lot of issues with this code (indentation, uninitialized values CleanChar, and using the conversion function at all) but if you are insistent on using the "convertToString" function you need to modify it so that it does not use all entries in the character array that may not be used up. Instead, this would be slightly better, although I wouldn't use it at all. Basically, you need to stop when you hit the null character in the character array / cstring.

string convertToString(char* a, int size) //character array to string function
{
    int i;
    string s = "";
    for (i = 0; i < size && a[i] != '\0'; i++) {
        s = s + a[i];
    }
    return s;
}
Shaun Ramsey
  • 562
  • 4
  • 14