HELP PLEASE

MY CODE DOES NOT WORK, AND I REALLY DONT KNOW WHY! I TRIED TO FIGURE OUT EVEN WITH PEN AND PAPER, BUT EVERYTHING SEEMS PERFECT EXCEPT OUTPUT.
I WANTED TO MAKE PROGRAME WHICH DOES THIS
I ENTER 2398, I GET 8932
I ENTER 5467, I GET 7645 AND SIMILAR.
MY CODE:
#include <iostream>
#include <math.h>
using namespace std;


int invbr =0;
int Inv3(int x)
{
int inverzno = (x%10) * 100 + ((x/10)%10) * 10 + x/100;
return inverzno;
}

int Inv(int u)
{
int f;
int p =1;
int j=1;
int brojac =0;
int y =u;

while(y>0)
{

y /=10;
brojac = brojac + 1;
}
cout << brojac << endl;
p=brojac -1;
for( p; p>=0; p-=1)
{


f = pow(10,p);
int q = u/j;


invbr += (q%10) * f;
j*= 10;
}

cout <<invbr <<endl;


}


int main()
{
int A;
cin >> A;
Inv(A);

return 0;
}




I personally would do it this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;
int main()
{
string word;
cout << "Enter something:  ";
cin >> word;
cout << endl;
for (int i = word.length(); i >= 0; i--)
{
     cout << word[i];
}
return 0;
}
@newbiee99

Sorry, dont wanna hijack OP's thread, but I cant seem to understand why your code actually works. If I enter a 3 digit number (like 238), i is initialized to 3 (because there are 3 characters), hence it first outputs word[3]. But how is this possible when the highest element number in a 3 character string is 2? The code seems to suggest that word[3] exists?
@ Arslan7041,

Very good question!
NO, word[3] does not exist. However, whenever you initialize an array with certain elements and try to access an element that is out of the array's size limit, the compiler makes that element and sets it equal to nothing.
So in this case, the element at word[3] is equal to "". (It's not space or endline) It is just 'no character'.
In an array of ints, that element would be 0 or 1 depending on the compiler.

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string array = "1234";
    cout << "'" << array[4] << "'" << endl;
    
    int myarr[4] = {1,2,3,4};
    cout << "'" << myarr[4] << "'" << endl;
    
    return 0;
}
Last edited on
That is false. In most cases, trying to access outside of the bounds of the size of a string with operator[] will result in an assertion failure. However, it's undefined behavior even if it does work.

http://codepad.org/2ybMdQQe
Last edited on
NO, word[3] does not exist. However, whenever you initialize an array with certain elements and try to access an element that is out of the array's size limit, the compiler makes that element and sets it equal to nothing.
So in this case, the element at word[3] is equal to "". (It's not space or endline) It is just 'no character'.
In an array of ints, that element would be 0 or 1 depending on the compiler.


If it is an array of characters (string) then the last element would be a null terminator ('\0') otherwise it is undefined not 0 or 1. Either way it is not the best practice.

As far as this problem the easiest solution would be something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main()
{
    std::string str = "";

    std::cout << "Please enter a string to be reversed: ";
    std::getline(std::cin, str);

    std::cout << "Before: " << str << '\n'
                  << "After: " << std::string(str.rbegin(), str.rend()) << std::endl;

    return 0;
}
Please enter a string to be reversed: 2398
Before: 2398
After: 8932
 


The reason this works is because of the range based constructor and reverse iterators.

Alternatively you could do something as simple as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main()
{
    int n = 0;
    
    std::cout << "Please enter a number to be reversed: ";
    std::cin >> n;
    
    std::cout << "Before: " << n << '\n'
              << "After: ";
    
    while(n) //while n has a value still
    {
        std::cout << n % 10; //grab right hand digit
        n /= 10; //remove right hand digit
    }
    
    std::cout << std::endl;
    
    return 0;
}
Please enter a number to be reversed: 2398
Before: 2398
After: 8932




EDIT: Accidentally forgot the second parameter (funnily enough I had it in the shell I was running but some how it pasted incorrectly, might have pasted old clipboard)
Last edited on
As far as this problem the easiest solution would be something like


Your first code does not seem to be working for me. I tried it both on the c++ shell and in codeblocks.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
using namespace std;

int main(){	
	string s;
	getline(cin,s);
	int i= s.size();
	while(i--) cout << s[i];
	
return 0;
}
giblit forgot the second parameter to the constructor of std::string.

Fixed version: http://ideone.com/kZEvcd
@gilbit: Can you briefly explain what the functions rbegin() and rend() do to reverse a string? What exactly is going on 'under the hood' when you use these two functions?
Last edited on
rbegin returns an Iterator type with operator++ overloaded. It returns an iterator to the end of the iterable container, and moves down on each increment.
@Arslan7041, as NoXzema mentioned they are just reverse iterators - iterate over the string backwards instead of forwards

Basically something like
1
2
3
4
5
while(end != begin)
{
    append character
    move last character iterator towards the beginning
}


Please check out http://www.cplusplus.com/reference/string/string/ for both the constructor and rbegin/rend


Last edited on
Sorry, still not getting it.

What exactly is going on "under the hood"? I dont know what "reverse iterator" means.

I've read the references and even googled it, but can't find a simple explanation.
Here's a small example which shows how to overload operator++ in order to iterate backwards through a character array.

http://codepad.org/vzR5ubse
Last edited on
Topic archived. No new replies allowed.