I was wondering if it's possible to ignore certain digits of a

number, and how it would be done.It will always be a 21 digit

number, and i want it to only use the last 7 digits of the number. Eg:

000J90120071000002291 is the number, and I only want it to use

0002291 for the program, and ignore the rest.

Thanks in advance.

Recommended Answers

All 13 Replies

please don't use TEX tags, just do number%10000000

Chris

this first converts int to string, then takes the wanted part.

#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
	int i = 123456789;
std::string s;
std::stringstream out;
out << i;
s = out.str();
string s2 =s.substr(0,3);
cout << s2 << endl;

	return 0;
}

Thank you.

is your problem solved?

Not yet, but i'm new to this, so it'll be a while before i've incorperated what you said into my program.
Also, since the number that will have to be changed will be a cin, could you show me another version with a cin?

Well i'm still really confused, so could i get you to show me how to put that in this script? it will have to work for w.

#include <iostream>   
 #include <sstream>     
 #include <string>            

using namespace std;
int main() { 
    long double w, t, p;    
    cout << "Please enter the price."; 
    cout << endl;   
    cin>>p;   
    t = 0;   
    cout << "Please scan the the weight.";  
    cin >>w; 
    cout << endl;  
    cout << endl;
    t = w * p + t;
    cout << endl; 
    cout <<"Total:"<<t;
    cout << endl;
    int i = 1000; while (i-- > 0) { cin >> w; t += w * p; cout << endl; cout<<"Total:"<<t; cout << endl; };
    cin.get();
    cin.get();
}

I still think tat you should look at the modulus symbol

I still think tat you should look at the modulus symbol

It's a 21 digit number. That's over 64 bits, so I can't think of a data type to put it into where you could use the modulus operator (%). I think it needs to be a string, then do a substring of the last seven digits, then convert to an integer.

string number21 = "123456789012345678901";
string number7  = number21.substr (14);
int number = atoi (number7.c_str ());

Should be equivalent to this line:

int number = 5678901;

http://www.cplusplus.com/reference/string/string/substr.html
http://www.cplusplus.com/reference/string/string/c_str.html
http://www.cplusplus.com/reference/clibrary/cstdlib/atoi.html

Fair enough, I wasn't taking into consideration the length of the number. Although yould I reccomend strtol() instead of atoi().

Chris

Or just use string streams.

Still has errors,

#include <iostream>   
 #include <sstream>     
 #include <string>            

using namespace std;
int main() { 
    long double t, p;    
    int w;
    cout << "Please enter the price."; 
    cout << endl;   
    cin>>p;   
    t = 0;   
    cout << "Please scan the the weight.";  
    cin >>w; 
    string number21 = w;
    string number7 = number21.substr (14);
    int number = atoi (number7.c_str ());
    cout << endl;  
    cout << endl;
    t = w * p + t;
    cout << endl; 
    cout <<"Total:"<<t;
    cout << endl;
    int i = 1000; while (i-- > 0) { cin >> w; t += w * p; cout << endl; cout<<"Total:"<<t; cout << endl; };
    cin.get();
    cin.get();
}

Returns the error,

invalid conversion from `int' to `const char*'

initializing argument 1 of `std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'

#include <iostream>   
 #include <sstream>     
 #include <string>            

using namespace std;
int main() { 
    long double t, p;    
    int w;
    cout << "Please enter the price."; 
    cout << endl;   
    cin>>p;   
    t = 0;   
    cout << "Please scan the the weight.";  
    cin >>w; 
    string number21 = w;
    string number7 = number21.substr (14);
    int number = atoi (number7.c_str ());
    cout << endl;  
    cout << endl;
    t = w * p + t;
    cout << endl; 
    cout <<"Total:"<<t;
    cout << endl;
    int i = 1000; while (i-- > 0) { cin >> w; t += w * p; cout << endl; cout<<"Total:"<<t; cout << endl; };
    cin.get();
    cin.get();
}

See red above. So the weight is a 21 digit number? An integer can't hold a 21 digit number, so this line is going to give you trouble:

int w;
cin >> w;

If the user types in 21 digits, w isn't going to be able to hold all of them. You are also going to have problems here:

string number21 = w;

number21 is a string. w is an integer. That's a mismatch. They should both be the same type if you are going to assign one to the other. Read the data from the user into a string, then convert:

string number21;
cout << "Please scan the the weight.";  
cin >> number21;
string number7 = number21.substr (14);
int w = atoi (number7.c_str ());

On a side note, if you are interested in strtol rather than atoi , as Freak Chris suggested, you can check out these links.

http://www.cplusplus.com/reference/clibrary/cstdlib/strtol.html
http://www.dreamincode.net/forums/showtopic2441.htm
http://en.wikipedia.org/wiki/Strtol

Freaky Chris is probably right. It's better to use strtol rather than atoi . You are responsible for ensuring that good data is passed to atoi . strtol is more helpful in catching bad data (i.e. if you accidentally passed it all 21 digits).

Thank you guys for the very descriptive help.

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.