I need help reversing the text. I haven't been able to get the tex

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1253 Views - Last Post: 16 January 2019 - 03:01 PM Rate Topic: -----

#1 Ruf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-April 16

I need help reversing the text. I haven't been able to get the tex

Posted 15 January 2019 - 10:53 PM

 int main(int argc, char* argv[])
{
    //- InMemoryReverse
    printf("--- InMemoryReverse ---\n");
    char reversibleText[] = "This text should be reversed.";
    InMemoryReverse(reversibleText, strlen(reversibleText));

	printf("InMemoryReverse: %s\n", reversibleText);

Is This A Good Question/Topic? 0
  • +

Replies To: I need help reversing the text. I haven't been able to get the tex

#2 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 954
  • View blog
  • Posts: 3,782
  • Joined: 13-June 14

Re: I need help reversing the text. I haven't been able to get the tex

Posted 15 January 2019 - 11:00 PM

You need to show us all the code (crucially the InMemoryReverse function) and explain where you're stuck or what isn't working and what you've done to debug the problem.
Was This Post Helpful? 0
  • +
  • -

#3 Ruf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-April 16

Re: I need help reversing the text. I haven't been able to get the tex

Posted 15 January 2019 - 11:07 PM

#include "targetver.h"

#include <stdio.h>
#include <cstring>
#include <thread>
#include <mutex>
#include <string>

std::mutex myMutex;
bool locked = true;

class ClassOne
{
public:
    ClassOne()
    {
        printf("ClassOne Constructor\n");
    }

    ~ClassOne()
    {
        printf("ClassOne Destructor\n");
    }

    void Function()
    {
        printf("ClassOne Function\n");
    }
};

class ClassTwo : public ClassOne
{
public:
    ClassTwo()
    {
        printf("ClassTwo Constructor\n");
    }

    ~ClassTwo()
    {
        printf("ClassTwo Destructor\n");
    }

    void Function()
    {
        printf("ClassTwo Function\n");
    }
};

void InMemoryReverse(char* buffer, int length)
{
	for (int index = 0; index < length; ++index)
    {
        auto temp = buffer[index];
        buffer[index] = buffer[length - index];
        buffer[length - index] = temp;
    }
}; 

void Multithreaded(int count)
{
    while (locked);

    printf("Multi-threaded: %d", count);
}

int main(int argc, char* argv[])
{
    //- InMemoryReverse
    printf("--- InMemoryReverse ---\n");
    char reversibleText[] = "This text should be reversed.";
    InMemoryReverse(reversibleText, strlen(reversibleText));

	printf("InMemoryReverse: %s\n", reversibleText);

    //- Object Oriented
    printf("--- Object Oriented ---\n");
    ClassOne one;
    one.Function();

    //- Multi-threaded
    printf("--- Multi-threaded ---\n");
    std::thread threads[10];
    for (int count = 0; count <= 10; ++count)
        threads[count] = std::thread(Multithreaded, count);

    locked = false;

    for (int count = 0; count < 10; ++count)
        threads[count].join();

	return 0;
}



I am stuck on the whole fact of I am not able to make changes to the code, We have to stick with what is given. I am new to working with this in C++, the whole using the reverse and actually producing the output correctly.

Should I change "buffer" after char or change the argv that is in the code under the InMemoryReverse
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 12:04 AM

You are reversing far too much. You reversed half the string and then keep on going to reverse it back on itself.
Was This Post Helpful? 2
  • +
  • -

#5 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2295
  • View blog
  • Posts: 4,397
  • Joined: 30-May 10

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 12:16 AM

> for (int count = 0; count <= 10; ++count)
You're also stepping off the end of your array with your <= test when creating threads.

> for (int count = 0; count < 10; ++count)
You get it right when you join though.

> #include <stdio.h>
This should be
#include <cstdio>
You do it right on the next line with cstring.

Though why aren't you using C++ iostream for printing?

Edit:
Also posted here -> http://www.cplusplus...general/248745/

This post has been edited by Salem_c: 16 January 2019 - 12:18 AM

Was This Post Helpful? 2
  • +
  • -

#6 ndc85430   User is offline

  • I think you'll find it's "Dr"
  • member icon

Reputation: 954
  • View blog
  • Posts: 3,782
  • Joined: 13-June 14

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 01:02 AM

Is there a reason your code mixes reversing a string with multithreading?
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 01:21 AM

View PostRuf, on 16 January 2019 - 01:07 AM, said:

I am stuck on the whole fact of I am not able to make changes to the code, We have to stick with what is given.

It would help us dramatically if you tell us what you can change and what you can't change. If you can't change anything at all, then you are stuck with the bugs that have been given to you.

View PostRuf, on 16 January 2019 - 01:07 AM, said:

Should I change "buffer" after char or change the argv that is in the code under the InMemoryReverse

No.

View PostRuf, on 16 January 2019 - 01:07 AM, said:

I am new to working with this in C++

Well, one quick bit of advice is not to mix in C and and C++. In general, if you are using C++, you should prefer to use C++ I/O (e.g. printf() is the C way of doing IO), and use C++ std::string instead of the current C-style string that you are playing with for your string reversing code. That means no strlen(), but rather you use std::string::size() to determine the length of a C++ string.
Was This Post Helpful? 2
  • +
  • -

#8 Ruf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-April 16

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 08:28 AM

Hey everybody, thank you for the input and help you gave.
With this code that I have attached above, I was told that "We would not have to change the values in strings to get the appropriate output"

So I am kind of lost in that sense because I have tried to change the code to a more friendly code, but was told that wasn't the best thing to do. Like i put above, I don't have to change the values to get the outputs. So am I looking over small changes? Or should I just make drastic changes to the code and go that route? Even though it wasnt suggested

View Postndc85430, on 16 January 2019 - 01:02 AM, said:

Is there a reason your code mixes reversing a string with multithreading?


This is just the code that we were given and told that we need to get it to produce the appropriate output. I am just as confused as you all might be, It really doesn't make sense to me some of the code in here and why it is included. I feel like there is an easier way for all of this instead of the way we were given.

Here is the output I am suppose to get. So far I only have it showing ClassOne Constructor and Destructor

"--- InMemoryReverse ---
InMemoryReverse: .desrever eb dluohs txet sihT
--- Object Oriented ---
ClassOne Constructor
ClassTwo Constructor
ClassTwo Function
ClassTwo Destructor
ClassOne Destructor
--- Multi-threaded ---
Multi-threaded: 0"
Was This Post Helpful? 0
  • +
  • -

#9 Ruf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-April 16

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 08:43 AM

View PostRuf, on 16 January 2019 - 01:07 AM, said:

I am new to working with this in C++

Well, one quick bit of advice is not to mix in C and and C++. In general, if you are using C++, you should prefer to use C++ I/O (e.g. printf() is the C way of doing IO), and use C++ std::string instead of the current C-style string that you are playing with for your string reversing code. That means no strlen(), but rather you use std::string::size() to determine the length of a C++ string.
[/quote]

When I make the change from strlen to std::string, It gives me an error. This whole code is just giving me fits, I truly do not understand why we were given such a code that seems so mixed up
Was This Post Helpful? 0
  • +
  • -

#10 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 10:54 AM

View PostRuf, on 16 January 2019 - 10:43 AM, said:

When I make the change from strlen to std::string, It gives me an error. This whole code is just giving me fits, I truly do not understand why we were given such a code that seems so mixed up

How exactly did you do that? You can't just do a drop in replacement. You need to instantiate a C++ string, and then call size() on that instance.

View PostRuf, on 16 January 2019 - 10:28 AM, said:

With this code that I have attached above, I was told that "We would not have to change the values in strings to get the appropriate output"

That means that your current approach of clobbering the input C string to reverse it in place is not what they are looking for. Instead they likely want you to return a C++ string that has the letters reversed. But then you never answered our question about what you are allowed to change and what you aren't allowed to change so it's really hard to tell what is being asked.

Perhaps post the complete assignment or project text instead of feeding it to us bit by bit.
Was This Post Helpful? 1
  • +
  • -

#11 Ruf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-April 16

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 10:56 AM

View PostSkydiver, on 16 January 2019 - 10:54 AM, said:

How exactly did you do that? You can't just do a drop in replacement. You need to instantiate a C++ string, and then call size() on that instance.


Oh well then maybe thats where I messed up, I just dropped in the std::string where strlen was. How would I go about making that change and adding a C++ string in the block of code for the reverse text

This post has been edited by Skydiver: 16 January 2019 - 11:06 AM
Reason for edit:: Fixed botched quote.

Was This Post Helpful? 0
  • +
  • -

#12 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 11:05 AM

Here's a dead simple example:
std::string name = "Ruf";
std::cout << "The length is " << name.size() << std::endl;


Was This Post Helpful? 1
  • +
  • -

#13 Ruf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-April 16

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 11:59 AM

Ok, thank you. And while you're replying and giving feedback. Can I ask another question? I haven't had much luck with calling the destructors of the program, I have the constructors from both classes. Here is my code so far, do I need to call for a delete at the end of my main? Lines 81-85 is what I have so far for the main code

#include "targetver.h"

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <thread>
#include <mutex>
#include <string>

std::mutex myMutex;
bool locked = true;
using namespace std;

class ClassOne
{
public:
    ClassOne()
    {
        printf("ClassOne Constructor\n");
    }

    ~ClassOne()
    {
        printf("ClassOne Destructor\n");
    }

    void Function()
    {
        printf("ClassOne Function\n");
    }
};

class ClassTwo : public ClassOne
{
public:
    ClassTwo()
    {
        printf("ClassTwo Constructor\n");
    }

    ~ClassTwo()
    {
        printf("ClassTwo Destructor\n");
    }

    void Function()
    {
        printf("ClassTwo Function\n");
    }
};

void InMemoryReverse(char* buffer, int length)
{
	for (int index = 0; index < length; ++index)
    {
        auto temp = buffer[index];
        buffer[index] = buffer[length - index];
        buffer[length - index] = temp;
    }
}; 

void Multithreaded(int count)
{
    while (locked);

    printf("Multi-threaded: %d", count);
}

int main(int argc, char* argv[])
{
    //- InMemoryReverse
    printf("--- InMemoryReverse ---\n");
    char reversibleText[] = "This text should be reversed.";
    InMemoryReverse(reversibleText, strlen(reversibleText));

	printf("InMemoryReverse: %s\n", reversibleText);

    //- Object Oriented
    printf("--- Object Oriented ---\n");
    ClassOne one; 
	ClassTwo two; 
    two.Function();

    //- Multi-threaded
    printf("--- Multi-threaded ---\n");
    std::thread threads[10];
    for (int count = 0; count <= 10; ++count)
        threads[count] = std::thread(Multithreaded, count);

    locked = false;

    for (int count = 0; count <= 10; ++count)
        threads[count].join();

	return 0;
}


This post has been edited by Skydiver: 16 January 2019 - 12:04 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6717
  • View blog
  • Posts: 22,931
  • Joined: 05-May 12

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 12:07 PM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

Destructor are normally called a the end of the scope that declares the instance. In your case above, the two classes will be destroyed at the end of main(). If you don't like that, define another scope and put the class instantiation there.
Was This Post Helpful? 0
  • +
  • -

#15 Ruf   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 33
  • Joined: 27-April 16

Re: I need help reversing the text. I haven't been able to get the tex

Posted 16 January 2019 - 12:09 PM

I am trying to have my Multi-Threaded code in main produce the output 0-9 but what I have doesn't produce any output. Just says
---Multi Threaded---

#include "targetver.h"

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <thread>
#include <mutex>
#include <string>

std::mutex myMutex;
bool locked = true;
using namespace std;

class ClassOne
{
public:
    ClassOne()
    {
        printf("ClassOne Constructor\n");
    }

    ~ClassOne()
    {
        printf("ClassOne Destructor\n");
    }

    void Function()
    {
        printf("ClassOne Function\n");
    }
};

class ClassTwo : public ClassOne
{
public:
    ClassTwo()
    {
        printf("ClassTwo Constructor\n");
    }

    ~ClassTwo()
    {
        printf("ClassTwo Destructor\n");
    }

    void Function()
    {
        printf("ClassTwo Function\n");
    }
};

void InMemoryReverse(char* buffer, int length)
{
	for (int index = 0; index < length; ++index)
    {
        auto temp = buffer[index];
        buffer[index] = buffer[length - index];
        buffer[length - index] = temp;
    }
}; 

void Multithreaded(int count)
{
    while (locked);

    printf("Multi-threaded: %d", count);
}

int main(int argc, char* argv[])
{
    //- InMemoryReverse
    printf("--- InMemoryReverse ---\n");
    char reversibleText[] = "This text should be reversed.";
    InMemoryReverse(reversibleText, strlen(reversibleText));

	printf("InMemoryReverse: %s\n", reversibleText);

    //- Object Oriented
    printf("--- Object Oriented ---\n");
    ClassOne one; 
	ClassTwo two; 
    two.Function();

    //- Multi-threaded
    printf("--- Multi-threaded ---\n");
    std::thread threads[10];
    for (int count = 0; count <= 10; ++count)
        threads[count] = std::thread(Multithreaded, count);

    locked = false;

    for (int count = 0; count <= 10; ++count)
        threads[count].join();

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3