Question

Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2...

Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2 - Stack around the variable 'string4b' was corrupted. I need some help because if the arrays for string4a and string4b have different sizes. Also if I put different sizes in string3a and string4b it will say that those arrays for 3a and 3b are corrupted. But for the assigment I need to put arrays of different sizes so that they can do their work without showing debugging errors. And in other debugging tools such as code blocks it works fine but for this assignment I need to use visual studio. The program does not need to use any of C-library such as strlen. I need to make my own function to swap the arrays. Thanks.

This is the part of the code where I have trouble:

#include <iostream>

using std::cout;

using std::cin;

using std::endl;

int myStrLen(const char*);

void myStrCpy(char*, const char*);

int myStrCmp(const char*, const char*);

void myStrSwap(char*, char*);

void myStrUpr(char*);

void myStrLwr(char*);

int main()

{

//STEP 3

char string3a[] = { "Those" };

char string3b[] = { "This" };

cout << "string3a = " << string3a << endl;

cout << "string3b = " << string3b << endl;

if (myStrCmp(string3a, string3b) == 0)

cout << "They are the same!" << endl << endl;

else

cout << "They are NOT the same!" << endl << endl;

//STEP 4

char string4a[] = { "Elsere" };

char string4b[] = { "Solohjj" };

cout << "string4a = " << string4a << endl;

cout << "string4b = " << string4b << endl;

myStrSwap(string4a, string4b);

cout << "string4a = " << string4a << endl;

cout << "string4b = " << string4b << endl << endl;

}

//STEP 3

int myStrCmp(const char* str1, const char* str2)

{

int i = 0; //initializes the integer array to 0 necessary for the loop to work

//loop to count ever character in the array and determine if they are not

//the same to return the value of '1'

for (i = 0; str1[i] != '\0' && str2[i] != '\0'; i++)

{

if (str1[i] != str2[i]) //if the characters from the arrays 'char string3a[]' and

{ //'char string3b[]' are not the same, it will return 1

return 1;

}

}

//loop to count every character in the array and determine if they have the same

//words and the same length before it encounters the null character

if (str1[i] == '\0' && str2[i] == '\0')

{

return 0; //if they have the same length and the same words it will return 0

} //if not, it will continue to return 1

else

{

return 1;

}

}

//STEP 4

void myStrSwap(char* str1, char* str2)

{

//loop to count every character in the arrays from main.

//the loop will swap every character and finishes until it reaches the null

//character in the arrays

int index = -1;

for (int i = 0; str1[i] != '\0' || str2[i] != '\0'; i++)

{

// if string 1 ends

if (str1[i] == '\0')

{

// find the position that string1 ends

if (index == -1)

index = i;

str1[i] = str2[i]; // copy the string2 data

// if the next position is end of string

if (str2[i + 1] == '\0')

{

// end both the string

str1[i + 1] = '\0';

str2[index] = '\0';

break; // break the loop

}

}

// if string 2 ends

else if (str2[i] == '\0')

{

// find the position that string1 ends

if (index == -1)

index = i;

// copy the string1 data

str2[i] = str1[i];

// if the next position is end of string

if (str1[i + 1] == '\0')

{

// end both the string

str2[i + 1] = '\0';

str1[index] = '\0';

break; // break the loop

}

}

else

{

char temp = str1[i];

str1[i] = str2[i];

str2[i] = temp;

}

}

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hi,

myStrSwap() function you have added two if conditions that are checking that str1[i] == '\0' and other is

str2[i] == '\0'. Now we consider first if condition, suppose str1[i] == '\0' means its end of the string. You can not add any data in that place.

Here,

string4a[] = E l s e r e // size is 7 bytes (including one byte of '\0')

string4b[] = S o l o h j j // size is 8 bytes (including one byte of '\0')

Noow for loop comes at i = 7

i.e string4a[7] is '\0' and you are modifying it. which is wrong. Therefore runtime error getting.

Add a comment
Know the answer?
Add Answer to:
Problem with C++ program. Visual Studio say when I try to debug "Run-Time Check Failure #2...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ Use the below program. Fill in the code for the 2 functions. The expected...

    Using C++ Use the below program. Fill in the code for the 2 functions. The expected output should be: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: The:fox:jumps:over:the:fence.: #include #include #include using namespace std; //Assume a line is less than 256 //Prints the characters in the string str1 from beginIndex //and inclusing endIndex void printSubString( const char* str1 , int beginIndex, int endIndex ) { } void printWordsInAString( const char* str1 ) { } int main() { char str1[] = "The fox jumps over the...

  • SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! myst...

    SCREENSHOTS ONLY PLEASE!!! DON'T POST ACTUAL CODE PLEASE LEAVE A SCREENSHOT ONLY! ACTUAL TEXT IS NOT NEEDED!!! mystring.h: //File: mystring1.h // ================ // Interface file for user-defined String class. #ifndef _MYSTRING_H #define _MYSTRING_H #include<iostream> #include <cstring> // for strlen(), etc. using namespace std; #define MAX_STR_LENGTH 200 class String { public: String(); String(const char s[]); // a conversion constructor void append(const String &str); // Relational operators bool operator ==(const String &str) const; bool operator !=(const String &str) const; bool operator >(const...

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    Hello, I have some errors in my C++ code when I try to debug it. I tried to follow the requirements stated below: Code: // Linked.h #ifndef INTLINKEDQUEUE #define INTLINKEDQUEUE #include <iostream> usingnamespace std; class IntLinkedQueue { private: struct Node { int data; Node *next; }; Node *front; // -> first item Node *rear; // -> last item Node *p; // traversal position Node *pp ; // previous position int size; // number of elements in the queue public: IntLinkedQueue();...

  • I am trying to run this program in Visual Studio 2017. I keep getting this build...

    I am trying to run this program in Visual Studio 2017. I keep getting this build error: error MSB8036: The Windows SDK version 8.1 was not found. Install the required version of Windows SDK or change the SDK version in the project property pages or by right-clicking the solution and selecting "Retarget solution". 1>Done building project "ConsoleApplication2.vcxproj" -- FAILED. #include <iostream> #include<cstdlib> #include<fstream> #include<string> using namespace std; void showChoices() { cout << "\nMAIN MENU" << endl; cout << "1: Addition...

  • In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete t...

    In C++ please! *Do not use any other library functions(like strlen) than the one posted(<cstddef>) in the code below!* /** CS 150 C-Strings Follow the instructions on your handout to complete the requested function. You may not use ANY library functions or include any headers, except for <cstddef> for size_t. */ #include <cstddef> // size_t for sizes and indexes ///////////////// WRITE YOUR FUNCTION BELOW THIS LINE /////////////////////// ///////////////// WRITE YOUR FUNCTION ABOVE THIS LINE /////////////////////// // These are OK after...

  • When I try to run the program it gives me an erron saying Expected expresion on...

    When I try to run the program it gives me an erron saying Expected expresion on this line, else if(phrase.find(sub_String)!=std::string::npos). #include #include #include using namespace std;    int main( ) {          // Defining variables. //Initilizing variables. int magicNum; int magicNum1; int position; int phraseLenght; string vowels = "aeiou"; string phrase; string sub;       // The phrase and the sub phrase cout << "Enter a phrase:" << endl; std::getline (std::cin,phrase);    cout << "Enter a 3-character sub:"...

  • Hi!, having trouble with this one, In this class we use visual studio, C++ language --------------------------------------------------------------...

    Hi!, having trouble with this one, In this class we use visual studio, C++ language -------------------------------------------------------------- Exercise #10 Pointers - Complete the missing 5 portions of part1 (2 additions) and part2 (3 additions) Part 1 - Using Pointers int largeArray (const int [], int); int largePointer(const int * , int); void fillArray (int * , int howMany); void printArray (const char *,ostream &, const int *, int howMany); const int low = 50; const int high = 90; void main()...

  • c++, I am having trouble getting my program to compile, any help would be appreciated. #include...

    c++, I am having trouble getting my program to compile, any help would be appreciated. #include <iostream> #include <string> #include <string.h> #include <fstream> #include <stdlib.h> using namespace std; struct record { char artist[50]; char title[50]; char year[50]; }; class CD { //private members declared private: string artist; //asks for string string title; // asks for string int yearReleased; //asks for integer //public members declared public: CD(); CD(string,string,int); void setArtist(string); void setTitle(string); void setYearReleased(int); string getArtist() const; string getTitle() const; int...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT