I'm needing help in adding commas to the number the user enters, some guidance or help would be appreciated. So far I have it where i store the first three digits and the last six digits and then simply format it.
#include<iostream>
using namespace std;
int main ( int argc, char * argv[] )
{
unsigned long long userInput;
int fthreeDigit;
cout << "Enter a long long number: " << endl;
cin >> userInput;
fthreeDigit = ( userInput / 1000 );
userInput %= 1000;
cout << "Your Number: " << fthreeDigit << "," << userInput << endl;
system("pause");
return 0;
}
Is this what you need? The locale will do this for you correctly.
#include <iostream>
using namespace std;
int main ( int argc, char * argv[] )
{
unsigned long long userInput;
int fthreeDigit;
cout << "Enter a long long number: " << endl;
cin >> userInput;
std::cout.imbue(std::locale(""));
std::cout << userInput << std::endl;
return 0;
}
EDIT:
I have two solutions. first without playing with numbers (recommended) and second (division).
first solution is:
#include <cstdlib>
#include <iostream>
#include <locale>
#include <string>
using namespace std;
struct my_facet : public std::numpunct<char>{
explicit my_facet(size_t refs = 0) : std::numpunct<char>(refs) {}
virtual char do_thousands_sep() const { return ','; }
virtual std::string do_grouping() const { return "\003"; }
};
/*
*
*/
int main(int argc, char** argv) {
cout<<"before. number 5000000: "<<5000000<<endl;
std::locale global;
std::locale withgroupings(global, new my_facet);
std::locale was = std::cout.imbue(withgroupings);
cout<<"after. number 5000000: "<<5000000<<endl;
std::cout.imbue(was);
cout<<"and again as before. number 5000000: "<<5000000<<endl;
return 0;
}
before. number 5000000: 5000000
after. number 5000000: 5,000,000
and again as before. number 5000000: 5000000
RUN SUCCESSFUL (total time: 54ms)
and second (not recommended) is :
double f = 23.43;
std::string f_str = std::to_string(f);
or this
int a = 1;
stringstream ss;
ss << a;
string str = ss.str();
Then you can use string::substr() string::find() string::find_first_of() and similar methods to modify and format your string.
a similar topic
If you really want (have to) divide: (I think my version is cleaner & more efficient than the others)
unsigned long long userInput;
std::stringstream ss,s0;
std::string nr;
std::cout << "Enter a long long number: " << std::endl;
std::cin >> userInput;
int input=userInput;
int digits;
while(input>999){
input=input/1000;
digits=userInput-input*1000;
int mdigits=digits;
while(mdigits<100){s0<<"0";mdigits*=10;}
std::string s=ss.str();
ss.str("");
ss<<","<<s0.str()<<digits<<s;
userInput=input;
s0.str("");
}
std::string sf=ss.str();
ss.str("");
ss<<input<<sf;
std::cout << "Your Number: " << userInput << ";" << digits <<";"<<ss.str()<<std::endl;
Enter a long long number: 12345678 Your Number: 12;345;12,345,678
Here is the brute force but may be easiest to understand way to get every thousand digits with the help of a vector.
#include<iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main ( int argc, char * argv[] )
{
long long userInput;
int fthreeDigit;
cout << "Enter a long long number: " << endl;
cin >> userInput;
vector <int> res; //use vector to store every 3 digits
while (userInput !=0)
{
fthreeDigit = userInput %1000;
res.push_back(fthreeDigit);
userInput = userInput / 1000 ;
}
std::reverse(res.begin(), res.end());
for (size_t i = 0; i < res.size()-1; ++i)
{
if (res[i] ==0)
{
cout << "000"<<",";
}
else
{
cout << res[i] << ",";
}
}
if (res[res.size()-1] == 0)
{
cout << "000";
}
else{
cout << res[res.size()-1];
}
cout <<endl;
cin.get();
return 0;
}
I tested this code with the following case:
Input: 123456 Output: 123,456
Input: 12 Output: 12
Input: 12345 Output: 12,345
Input: 1234567 Output: 1,234,567
Input: 123456789 Output: 123,456,789
Input: 12345678 Output: 12,345,678
I guess this is what you want according to your response to comments.
You could do this:
#include <iostream>
#include <string>
using namespace std;
string commify(unsigned long long n)
{
string s;
int cnt = 0;
do
{
s.insert(0, 1, char('0' + n % 10));
n /= 10;
if (++cnt == 3 && n)
{
s.insert(0, 1, ',');
cnt = 0;
}
} while (n);
return s;
}
int main()
{
cout << commify(0) << endl;
cout << commify(1) << endl;
cout << commify(999) << endl;
cout << commify(1000) << endl;
cout << commify(1000000) << endl;
cout << commify(1234567890ULL) << endl;
return 0;
}
Output (ideone):
0
1
999
1,000
1,000,000
1,234,567,890
// Accepts a long number, returns a comma formatted string
CString num_with_commas(long lnumber)
{
CString num;
num.Format(%d",lnumber);
if(num.GetLength() > 3) {num.Insert(num.GetLength()-3, ',');}
if(num.GetLength() > 7) { num.Insert(num.GetLength()-7, ','); }
if (num.GetLength() > 12) { num.Insert(num.GetLength()-12, ','); }
return(num);
}
Related
I have attempted to remove the occurrences of a user inputted letter after they've chosen a word however, the final output prints out a random string of letters and numbers instead of what I expected. For example, if the user enters the text "Coffee" then proceeds to enter the letter "f", the program should return "Coee" as the final print. However, this is not the case. Could anyone check to see where I've gone wrong? Much obliged.
#include <iostream>
#include <string>
using namespace std;
void removeAllOccurrence(char text[], char letter)
{
int off;
int i;
i = off = 0;
if (text[i] == letter)
{
off++;
}
text[i] = text[i + off];
}
int main() {
string text;
char letter;
string newText;
cout << "Type your text: " << endl;
cin >> text;
cout << "Choose the letters to remove: " << endl;
cin >> letter;
cout << "your new text is: " << removeAllOccurrence << endl;
system("pause");
return 0;
}
This should do the job
#include <algorithm>
#include <string>
#include <iostream>
void remove_char(std::string s, char r) {
s.erase( std::remove( s.begin(), s.end(), r), s.end()) ;
std::cout << s << std::endl;
}
int main()
{
std::string test = "coffee";
char r = 'f';
remove_char(test, r);
return 0;
}
If u want to do this by hand try this:
std::string removeAllOccurrence(string text, char letter)
{
int off;
int i;
i = off = 0;
string out = "";
for (i = 0; i < text.size(); i++)
{
if (text[i] != letter)
{
out += text[i];
}
}
return out;
}
int main(void)
{
string text;
char letter;
string newText;
cout << "Type your text: " << endl;
cin >> text;
cout << "Choose the letters to remove: " << endl;
cin >> letter;
cout << "your new text is: " + removeAllOccurrence(text, letter) << endl;
system("pause");
return 0;
}
As you can see your main function was kinda right. You just need to pass some arguments into the function. Additonally you missed a loop in your remove function. If you use string in your main, why don't use string in yur function? You can just use string there, too
Kind Regards
I cannot figure out why my getchar() function is not working the way I want it to work. I am getting 10 not 2. Please take a look.
Main():
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main() {
int var, newvar;
cout << "enter a number:" << endl;
cin >> var;
newvar = getchar();
cout << newvar;
return 0;
}
Here is my output:
enter a number:
220
10
Ultimately though I need to be able to distinguish between a '+' '-' or letter or number.
This is maybe not the cleanest way to do it but you can get every char one by one :
#include <iostream>
using namespace std;
int main()
{
int var;
cout << "enter a number:" << endl;
cin >> var;
std::string str = to_string(var);
for(int i=0; i < str.length();++i)
cout << str.c_str()[i] << endl;
return 0;
}
If you enter for example: "250e5" it will get only 250 and skip the last 5.
Edit:
This is just a simple parser and does not do any logic.
If you want to make a calculator I would recommend you to look at what Stroustrup did in his book the c++ programming language.
int main()
{
string str;
cout << "enter a number:" << endl;
cin >> str;
for(int i=0; i < str.length();++i) {
char c = str.c_str()[i];
if(c >= '0' && c <= '9') {
int number = c - '0';
cout << number << endl;
}
else if(c == '+') {
// do what you want with +
cout << "got a +" << endl;
} else if(c == '-')
{
// do what you want with -
cout << "got a -" << endl;
}
}
return 0;
}
I was wondering how I could have a program output one of two strings at random.
Hard to explain but this is my example:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string age;
cout << "How old are you?" << endl;
cin >> age;
if (age < 0)
{
cout << "Invalid age" << endl;
}
if (0 >= age && age <<= 3)
{
cout << "Oh you're just a baby" << endl;
// OR (random)
cout << "Time to take a nap!" << endl;
}
return 0;
}
I want to program to output either, "Oh, you're just a baby" or "Time to take a nap!" at random whenever the user inputs a number between 0 and 3. Can anybody explain this?
try :
#include <cstdlib>
...
...
...
/* initialize random seed: */
srand(time(NULL));
/* generate secret number: */
int ss = rand() % 2;
if (ss) {
cout << "Oh you're just a baby" << endl;
} else {
cout << "Time to take a nap!" << endl;
}
You can also try this:
take input from user and compare it to random generated value and print output according to it:
#include <iostream>
using namespace std;
int main()
{
int age;
srand(time(NULL));
int a = rand()%3;
cout<<a<<endl; // print the random generated number (between 0 and 3 )
cout << "How old are you?" << endl;
cin >> age;
if (age < 0)
{
cout << "Invalid age" << endl;
}
if(age == a){
cout << "Oh you're just a baby" << endl;
}else{
cout << "Time to take a nap!" << endl;
}
return 0;
}
This uses the STL and only does the random selection from a table, without any user input:
#include <array>
#include <chrono>
#include <cstddef>
#include <iostream>
#include <random>
using std::cout;
using std::endl;
int main(void) {
// The size of the table:
constexpr size_t n = 2;
// A table of insults:
constexpr std::array<const char*, n> insults = {
"Oh, you're just a baby!",
"Time to take a nap."
};
// Boilerplate to initialize a RNG:
const std::default_random_engine::result_type seed = std::chrono::system_clock::now().time_since_epoch().count();
std::default_random_engine generator (seed);
// Generate a random index into the string table:
std::uniform_int_distribution<size_t> distribution(0, n-1);
// A random number from distribution:
const size_t x = distribution(generator);
// Our random string:
const char* const s = insults[x];
cout << s << endl;
return EXIT_SUCCESS;
}
I am having an issue trying to convert a number into a string. The purpose is for error checking to make sure the number is of a specific length. I have tried using both to_string() and convert.str() functions but get the same error back when trying to compile.
I am using MinGw g++ to compile and realize I need to tell it I want the C++11 standard, which I believe I have done. My compiler code is as follows:
NPP_SAVE
CD $(CURRENT_DIRECTORY)
C:\MinGW\bin\g++ -std=c++11 "$(FULL_CURRENT_PATH)" -o "$(NAME_PART).exe"
cmd /c $(NAME_PART).exe
Now assuming that is correct, my code for using to_string() is as follows:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
int book_code = 0;
cout << "Please enter the four digit book code: ";
cin >> book_code;
string code = to_string(book_code);
while (!(cin >> book_code) || code.length() != 4){
cin.clear();
cin.ignore(10000, '\n');
cout << "That is not a valid code." << endl;
cout << "Please enter the four digit book code: ";
}
}
And my code for using convert.str() is as follows:
int main() {
int book_code = 0;
cout << "Please enter the four digit book code: ";
cin >> book_code;
ostringstream Convert;
convert << book_code;
string code = convert.str();
while (!(cin >> book_code) || code.length() != 4){
cin.clear();
cin.ignore(10000, '\n');
cout << "That is not a valid code." << endl;
cout << "Please enter the four digit book code: ";
}
}
Neither of these was successful and both returned
error: 'to_string' was not declared in this scope
Am I missing something obvious?
In MinGW std::to_string() does not exist, you should declare your own implementation.
std::string to_string(int i)
{
std::stringstream ss;
ss << i;
return ss.str();
}
I recommend you to use MSYS2, it is more actualizated and you can avoid this type of problems.
Edit:
Checking the dot position in double:
#include <iostream>
#include <sstream>
#include <string>
std::string to_str_with_dot_pos(double i, unsigned int &pos)
{
std::stringstream ss;
ss << i;
std::string result(ss.str());
pos = 0;
while (pos < result.length() && result[pos] != '.') {
pos += 1;
}
return result;
}
int main(int argc, char **argv)
{
double d(12.54);
unsigned int pos(0);
// str should be "12.54".
// pos should be 2.
std::string str = to_str_with_dot_pos(d, pos);
std::cout << "double as string: " << str << std::endl;
std::cout << "double dot position: " << pos << std::endl;
return 0;
}
Explanation of the code (the while loop):
It gets every character of the std::string and checks if it does not equals to the . dot character, if the character is not equal to . it will add +1 to the pos variable.
It returns 2 and not 3 because we're counting from 0, not 1.
Also, this question is a duplicate.
Check that your version of MinGw support to_string, as the code above compiles correctly.
I'd recommend a different approach for length checking, one that avoids using strings:
#include <iostream>
#include <cmath>
using namespace std;
int is_len(int number, int len)
{
if(pow(10, len-1) <= number && number < pow(10, len))
return true;
return false;
}
int main()
{
int number = 1000;
cout << is_len(1, 2) << endl;
cout << is_len(1005, 4) << endl;
cout << is_len(9999, 4) << endl;
cout << is_len(599, 4) << endl;
cout << is_len(1005, 5) << endl;
return 0;
}
Prints:
0
1
1
0
0
i have the next code which asks the user for a really long number like 100000000 and then it prints how many times a given digit appears on that number, the code works fine and does everything correctly, but the professor told me that i dont have to use strings or chars, but when the code asks the user for a number it necessarily needs a string and i donĀ“t know how to modify it, i used the gmp library
#include <iostream>
#include <stdio.h>
#include <gmp.h>
#define MAX 40
using namespace std;
void searchDigit(FILE *fd);
int NewNumber();
int main()
{
FILE *fd;
int otherNumber;
string text;
mpz_t num;
do
{
if((fd = fopen("File.txt","w+"))!= NULL)
{
mpz_init(num);
cout << "Give me the number: " << endl;
cin >> text;
mpz_set_str(num,text.c_str(),10);
mpz_out_str(fd,10,num);
fclose(fd);
searchDigit(fd);
otherNumber = NewNumber();
}
else
cout << "Fail!!" << endl;
}while(otherNumber);
return 0;
}
void searchDigit(FILE *fd)
{
int car,continue = 1,r;
char answer,digit;
if((fd = fopen("File.txt","r"))!= NULL)
{
do
{
r = 0;
fseek(fd,0,SEEK_SET);
cout << "What digit do you want to search? " << endl;
cin >> digit;
while((car = fgetc(fd))!= EOF)
{
if(car == digit)
r++;
}
cout << "The digit x=" <<digit<< " appears " << r << " times" << endl;
cout << "Do you want to search any other digit? " << endl;
cin >> answer;
if(answer != 'S')
continue = 0;
}while(continue);
}
else
cout << "Fail!!" << endl;
}
int NewNumber()
{
char answer;
cout << "DO you wish to work with a new number? " << endl;
cin >> answer;
if(answer == 'S' || answer == 's')
return 1;
else
return 0;
}
Thanks in advance
Depends on how big your input might actually be... but for retrieving digits you could do something like:
#include <iostream>
using namespace std;
typedef unsigned long long UINT64;
int main() {
UINT64 i;
std::cin >> i;
while (i >= 1) {
int digit = i % 10;
std::cout << digit << " ";
i /= 10;
}
}
input: 18446744073709551614
outputs: 4 1 6 1 5 5 9 0 7 3 7 0 4 4 7 6 4 4 8 1