Omnimaga

General Discussion => Technology and Development => Computer Programming => Topic started by: Munchor on March 16, 2011, 05:28:27 pm

Title: C++ Questions
Post by: Munchor on March 16, 2011, 05:28:27 pm
http://pastebin.com/Ku8EWBXN (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?
Title: Re: C++ Questions
Post by: Ashbad on March 16, 2011, 05:34:18 pm
this should probably work.

http://pastebin.com/xpzM8rri
Title: Re: C++ Questions
Post by: Munchor 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;
}
Title: Re: C++ Questions
Post by: Ashbad 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!
Title: Re: C++ Questions
Post by: Munchor 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!
Title: Re: C++ Questions
Post by: Binder News on March 17, 2011, 02:56:42 pm
The printf is not defined in the above sample. You should use cout instead.
Title: Re: C++ Questions
Post by: DrDnar 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.
Title: Re: C++ Questions
Post by: Munchor 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.