is there a way to tell the program to remove a certain charecter or number from a string with out giving it a position. for example i have a program that asks the user to enter a set of numbers and the program reverses the order of the numbers. so if the user entered 12340 theprogram should print out 4321. but my program prints out 04321. i cant make it not print out the zero when it is at the front. but I need it to print out the zeros when they are at the end. for example if the user enters 01234 the program needs to print out 43210. you see. I need help please.

read user's input as string, so your normal reverse should print 43210 when input is 01234. to treat the other case, just remove trailing zeros after you read input before you perform reverse, because they will unwantedly appear in the front later otherwise:

ex:

char* removeTrailingZero(char* number){
    int i = strlen(number)-1;
    while (number[i]=='0' && i>0) i--;
    number[i] = '\0';
    return number;
}

best,
Harley

What I would do is flip the number like you mentioned, but instead convert the number back to an integer with a stringstream.

std::stringstream myStream;
int completeNumber;
myStream << backwardsNumber;
myStream >> completeNumber;

When you convert the number back to an integer, any leading 0s will automatically be snipped off.

I'm assuming you meant a C++ string...

hey could u show me how that would fit in my code. that would help me alot

#include "stdafx.h"
#include <iostream>
#include <ctype.h>
#include <fstream>
#include <string>
using namespace std;

int reverseDigit, integer, reverse; 


int main()
{

    string integer;
    int cntr;
    cout <<"Please input an integer: ";
    cin >> integer;
        
    cntr = integer.size();
    cntr--;

    cout <<"reverse = ";
    if (integer [0] == '-')
        {
            cout << "-";
        }
    while (cntr > -1)
        {    
            if (integer [cntr] == '-')
                cntr--;
            else 
                cout << (integer[cntr--]);
        }
    cout << endl;
    return 0;

o and yes C++ string

Probably the best idea would be to store the reversed number in a string before outputting it to the screen on-the-fly. That way you can manipulate the string anyway you want.

Or you could simply do the leading 0s check before you print out the char; you'd need a bool to keep track of whether you've hit a nonzero number, and if that's false, you basically discard any 0s.

yeah this is al going way over my head i am new to this. could u show me were it would go in my program. that would help me alot. i posted my prograam up top u can run it and see how it works if you would like. thaks

yeah this is al going way over my head i am new to this. could u show me were it would go in my program. that would help me alot. i posted my prograam up top u can run it and see how it works if you would like.

Look, I'm not going to add all the lines of code necessary so that your project is completed without you doing any of the work. I'll show you how to do my first solution, which is to store the numbers in a string, convert it to an integer, and then output the result.

First create a string that will hold the reversed numbers.

string reversedNumbers;

Then instead of printing out the character like you did here:

cout << (integer[cntr--]);

Put it in the string like so:

reversedNumbers += integer[/* you know what goes here*/];

Then pop it into a stream and back out like I showed in the first example.

ok i have done all that and now i cant get it to print out.

How about showing us your updated code. And please use code tags this time around.

ok how do i use code tags

#include "stdafx.h"
#include <iostream>
#include <ctype.h>
#include <fstream>
#include <string>
using namespace std;


int reverseDigit, integer, reverse; 


int main()
{ 
    string integer, reversedNumbers;
    int cntr;
    cout <<"Please input an integer: ";
    cin >> integer;
  
    cntr = integer.size();
    cntr--;

    cout <<"reverse = ";

    if (integer [0] == '-')
        {
            cout << "-";
        }
    while (cntr > -1)
        {    
            if (integer [cntr] == '-')
                cntr--;
            else 
                reversedNumbers += integer[cntr--];
            //cout << reversedNumbers;
        }
    cout << endl;
    
    return 0;


}

o and i commented out the cout. because if u run it without the cout uget nothing and if u run it with the cout u get extra numbers, i think it is the position in the string. if u run it both ways u will see what i am talking about.

Well? What did you expect? C++ isn't just going to automatically print out variables; you have to explicity state that you want them printed out. If programming was that automatic, us programmers would be out of a job.

Anyway, reread my post above. Convert to an integer by using a stringstream, and then print out the value.

ok and is going to be used before int main or in the program it self, because i am getting an error about it. and I dont no what it is trying to tell me, this is the error error C2079: 'myStream' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'
std::stringstream myStream;

I forgot to tell you that you need

#include <sstream>

If you want to use the method I mentioned previously.

ok i am getting an error on the

std::stringstream myStream;

the error is: error C2079: 'myStream' uses undefined class 'std::basic_stringstream<_Elem,_Traits,_Alloc>'
were is this need to be in the code above int main or in the code its self?

o ok

ok i am getting no errors, but it is still not printing out to the screen. i have never used myStream is this print out to the screen or is it just the name of a string i am confused. sorry about all the questions.

Reread the last sentance of my post-before-my-last-post. Once you convert it to an integer, you have to print it out.

If that's not working, try showing us the code you've got right now...

This is what i have

#include "stdafx.h"
#include <iostream>
#include <ctype.h>
#include <fstream>
#include <string>
using namespace std;
#include <sstream>

int reverseDigit, integer, reverse; 


int main()
{

    string integer, reversedNumbers;
    int cntr, completeNumber, backwardsNumber;
    cout <<"Please input an integer: ";
    cin >> integer;
    
    cntr = integer.size();
    cntr--;

    cout <<"reverse = ";

    if (integer [0] == '-')
        {
            cout << "-";
        }
    while (cntr > -1)
        {    
            if (integer [cntr] == '-')
                cntr--;
            else 
                reversedNumbers += integer[cntr--];
            std::stringstream myStream;
            myStream << backwardsNumber;
            myStream >> completeNumber;
        }
    cout << endl;
    
    return 0;


}

do i need a cout << after the myStreams

> do i need a cout << after the myStreams
Yes.

ok i am not at school any more so i dont have a compiler so i am going to have to work on it more tomorrow. but one last ?. what wil i cout because when i cout<< completeNumber; it did not work, am i couting the wrong thing?

Actually scratch that. It should be istringstream instead; will work much better.

int main()
{
    
    string integer, reversedNumbers;
    int cntr, completeNumber, backwardsNumber;
    cout <<"Please input an integer: ";
    cin >> integer;
    
    cntr = integer.size();
    cntr--;
    
    cout <<"reverse = ";
    
    if (integer [0] == '-')
    {
        cout << "-";
    }
    while (cntr > -1)
    {    
        if (integer [cntr] == '-')
            cntr--;
        else 
            reversedNumbers += integer[cntr--];

    }
    // notice how I moved this block of code down here
    std::istringstream myStream;
    myStream.str(reversedNumbers);
    myStream >> completeNumber;
    cout << completeNumber << endl;
    
    return 0;
    
    
}

Ok and will

#include <sstream>

work for

std::istringstream myStream;

Yup.

ok thanks alot for all the help i will run it tomorrow. i have some things left to do like not accept chars but i will work on it tomorrow i might have a few more ? but thanks for the help tonight.

ok i am having more problems. when u run the code with a number like 1234567890 it prints out 987654321 and that is fine. but if you enter something like 123456789123456789 it prints out something else. can someone take a look and tell me what they see. thanks

#include "stdafx.h"
#include <iostream>
#include <ctype.h>
#include <fstream>
#include <string>
using namespace std;
#include <sstream>

int integer, reverse;


int main()
{
    string integer, reversedNumbers;
    int cntr, completeNumber, backwardsNumber;
    cout <<"Please input an integer: ";
    cin >> integer;
    //This bellow takes the interger that the user typed in and determens the size of the string and flips it to reverse order if there is no negative.
    cntr = integer.size();
    cntr--;
    //This bellow determens wither or not the integer will have a negative or not and then flips the integer around to print it out in reverse order.
    cout <<"reverse = ";
    if (integer [0] == '-')
    {
        cout << "-";
    }
    while (cntr > -1)
    {
        if (integer [cntr] == '-')
            cntr--;
        else
            reversedNumbers += integer[cntr--];
    }
    // This bellow alows the program to take the zeros off the front of the integer when they are read back in reverse order and a zero is in front
    std::istringstream myStream;
    myStream.str(reversedNumbers);
    myStream >> completeNumber;
    cout << completeNumber << endl;
    
    return 0;
}

anyonr got anything?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.