0

So the program is supposed to use a loop to count the total size of the array of characters that contains "Hello World!", return that count as an int, and then call a function that uses pointers to start at the beginning and end of the array to swap the characters until the null point " " in between the words. I have largely figured out the code, but it won't compile correctly and won't display or won't complete the swap, I keep getting the following error: "LNK1168 cannot open Lab06.exe for writing"
How can I fix this?

#include "targetver.h"
#include <iomanip>
#include <array>
#include <stdio.h>
#include <tchar.h>
#include <string>
using namespace std;

int main()
{
    //int to hold size of array
    int count = 0;
    // declare a c-string to reverse
    char myString[] = "Hello world!";
    //print array as is
    cout << myString << endl;
    //find size of the array


    // call the reverser function
    reverser(myString, count);

    // output the result
    cout << myString << endl;

    system("PAUSE");
    return 0;
}

int findSize(char myString[], int count)
{

    count = (unsigned)strlen(myString);
    cout << "The size of the Array is: " << count << endl;
    return count;

}


void reverser(char myString[], int count)
{
    findSize(myString, count);
    char *strPtr;
    char *endPtr;
    endPtr =& myString[count];
    strPtr = myString;

    for (int x = 0; x <= count; x++)
    {
        strPtr++;
        *strPtr = myString[x];
        endPtr--;
        *endPtr = myString[count--];
        *strPtr = *endPtr;
        cout << strPtr << endl;
        cout << endPtr << endl;
    }

}
25
  • first by showing the error message(s) u are getting Commented Jul 19, 2017 at 23:52
  • 2
    "LNK1168 cannot open Lab06.exe for writing Means your last build is likely still running. Or your antivirus is blocking you. Commented Jul 19, 2017 at 23:53
  • 2
    @drescherjm - more likely the last run. Commented Jul 19, 2017 at 23:54
  • 1
    @drescherjm it would be UB if he passed in count as intended but in fact he passes in 0 Commented Jul 19, 2017 at 23:59
  • 1
    Reboot your PC. Commented Jul 20, 2017 at 0:00

2 Answers 2

2

OK lets start at the top

findsize returns the size, but you are ignoring it. You try to pass in count as an argument and hope to update count in findsize. This isnt how C works. So first do

int findSize(char myString[])
{

    int count = strlen(myString);
    cout << "The size of the Array is: " << count << endl;
    return count;
}

now when you call findsize you must remember the result.

int count = findsize(myString);

This return a count of the length. Say the string is 'abc', count will be 3.

The string has 3 chars myString[0], mystring[1] and ystring[2]. Note that there is no [3] character..

Reversing in place is tricky.

void reverser(char myString[])
{
    int count = findSize(myString);
    char *strPtr = myString
    char *endPtr = &myString[count - 1];
    strPtr = myString;

    for (int x = 0; x <= count / 2; x++)
    {
       char swap = *strPtr;
       strPtr[x] = *endPtr;
       *endPtr = swap;
       endPtr--;
    }
}

Disclaimer - I have not tested this code.

Sign up to request clarification or add additional context in comments.

2 Comments

Tried the code, '!H!!!!!orld!' instead of reversed array...not quite sure how that happened
You need to decrement endPtr each loop. --endPtr; after *endPtr = swap;
0
count = findSize(myString, count);

since findSize returns count you need count to equal the value it returns.

void reverser(char myString[])
{
int count = findSize(myString);
char *endPtr;
endPtr =& myString[count-1];
char *temp = new char[count];

for (int x = 0; x < count; x++)
{
     temp[x] = *endPtr;//this assgins the last letter of myString to the first element of temp 
     endPtr--;//decrement so endPtr points to myString[count - 1];
}
//now transfer
for(int x = 0; x < count; x++){
    myString[x] = temp[x];
}
delete [] temp;
}

20 Comments

code won't run. char temp messes up Line 43: Error expression must have a constant value Line 43: Error C2131 expression did not evaluate to a constant Line 47: Error C3863 array type 'char [count]' is not assignable
created completely new program, ported over the code, same errors. The errors occur at the line with char temp[count + 1]; and again at temp[x] = *endPtr;
@jonthie instead of char temp[count +1] try putting char temp[13
VLA is a non standard extension supported by gcc not by msvc
no. Defining another int as count+1 and then defining a const int as the first int does not work. putting const outside function also doesn't work as it is a 'non-numerical function'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.