Author Topic: C++ Questions  (Read 3571 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
C++ Questions
« on: March 16, 2011, 05:28:27 pm »
http://pastebin.com/Ku8EWBXN

I have that code and for some reason it is crashing when running, there is no output, it crashes right after executing.

What do you think about it?

Ashbad

  • Guest
Re: C++ Questions
« Reply #1 on: March 16, 2011, 05:34:18 pm »
this should probably work.

http://pastebin.com/xpzM8rri

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: C++ Questions
« Reply #2 on: March 17, 2011, 10:48:09 am »
That crashed Ashbad, and so did this:

Code: [Select]
#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
    string helloW = "Hello World";
    int i=helloW.length();
    i=i-1;
    for (i;i>=0;i--)
    {
        if (i<helloW.length())
        {
             printf("%s",helloW[i-1]);       
        }
    }
    cout << helloW.length() << "/n";   
    cout << helloW << "/n";
    cin.get();
    return 0;
}

Ashbad

  • Guest
Re: C++ Questions
« Reply #3 on: March 17, 2011, 01:34:00 pm »
well obviously this shows that I'm a nub in C++ and unfortunately can't help you :( hope someone else can, though.  Good luck with finding out where you went wrong!

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: C++ Questions
« Reply #4 on: March 17, 2011, 02:51:23 pm »
well obviously this shows that I'm a nub in C++ and unfortunately can't help you :( hope someone else can, though.  Good luck with finding out where you went wrong!
I'm sure you're pretty good, but thanks anyways!

Offline Binder News

  • LV8 Addict (Next: 1000)
  • ********
  • Posts: 785
  • Rating: +46/-3
  • Zombie of Tomorrow
    • View Profile
Re: C++ Questions
« Reply #5 on: March 17, 2011, 02:56:42 pm »
The printf is not defined in the above sample. You should use cout instead.
Spoiler For userbars:







Hacker-in-training!   Z80 Assembly Programmer     Axe Programmer
C++ H4X0R             Java Coder                           I <3 Python!

Perdidisti ludum     Cerebrum non habes

"We are humans first, no matter what."
"Fame is a vapor, popularity an accident, and riches take wings. Only one thing endures, and that is character."
Spoiler For Test Results:





Offline DrDnar

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 546
  • Rating: +97/-1
    • View Profile
Re: C++ Questions
« Reply #6 on: March 17, 2011, 02:59:08 pm »
cout is standard is C++ courses; printf is standard in C. You shouldn't mix the two if you can help it. C++ streams support output settings to accomplish the output formatting C's printf function supports.
"No tools will make a man a skilled workman, or master of defense, nor be of any use to him who has not learned how to handle them, and has never bestowed any attention upon them. . . . Yes, [] the tools which would teach men their own use would be beyond price."—Plato's The Republic, circa 380 BC

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: C++ Questions
« Reply #7 on: March 17, 2011, 03:58:25 pm »
So, there are 2 ways of doing it:

Code: [Select]

#include <iostream>
#include <string>
#include <stdio.h>
 
using namespace std;
 
int main() {
    string helloW = "Hello World";
    int i=helloW.length();
    i=i-1;
    for (i;i>=0;i--)
    {
        if (i<helloW.length())
        {
             printf("%s",helloW[i]);       
        }
    }
    cout << helloW.length() << "/n";   
    cout << helloW << "/n";
    cin.get();
    return 0;
}

Code: [Select]
#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
    string helloW = "Hello World";
    int i=helloW.length();
    i=i-1;
    for (i;i>=0;i--)
    {
        if (i<helloW.length())
        {
             cout << helloW[i];       
        }
    }
    cout << helloW << "\n";
    cin.get();
    return 0;
}

Note: That was not the problem, the problem was the 'i' and 'i-1' but telling me that actually made me realise what the error in this piece of code was.

EDIT:

Here is the final code to invert a string input by the user:

Code: [Select]
#include <iostream>
#include <string>
 
using namespace std;
 
int main() {
    string input = "";
    string finalString = "";
    getline(cin, input);
    int i=input.length();
    i=i-1;
    for (i;i>=0;i--)
    {
        if (i<input.length())
        {
             finalString+=input[i];
        }
    }
    cout << finalString;
    cin.get();
    return 0;
}

-------------------------------------
I know want to know how to slow down a C++ loop.
« Last Edit: March 17, 2011, 04:20:36 pm by Scout »