Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm need to create id just number. For example: 12345678
If user inputs fail( contain char), delete char immediately.
For example: 123a =>input gain!
Please help me!
I think this is what you are looking for:
#include <iostream>
#include <string>
#include <cctype>
int main () {
std::string input;
bool valid;
do {
valid = true;
std::cin >> input;
for (char c : input)
if (! std::isdigit( static_cast<unsigned char>(c) ) )
valid = false;
} while (! valid);
// Here the string is guaranteed to be valid
}
Be aware though, that whatever you are trying to do, this does not look like the proper way to do it. There are ways to read numbers in c++, and this is not what I would recommend.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
Please let me know how can I check whether the first word of a given string is "echo" ,ignoring if any spaces before the word.
Example:
string hello = " echo hello hihi";
if(startwith(hello, "echo")
{
//some code here
}
Please help me if possible
string_view has a similar functionality. Just skip the white space and use that.
#include <string>
#include <string_view>
using std::string, std::string_view;
constexpr bool StartsWithSkipWs(string_view const str,
string_view const prefix) noexcept {
auto begin = str.find_first_not_of(" \t\n\f\r\v");
if (begin == string_view::npos) return false;
return str.substr(begin).starts_with(prefix);
}
int main() {
string hello = "echo hello hihi";
if (StartsWithSkipWs(hello, "echo"))
{
// ...
}
}
#include<iostream>
#include<boost/algorithm/string.hpp>
using namespace std;
int main(){
string hello = " echo hello hihi";
boost::trim_left(hello);
string subst=hello.substr(0,4);
if(subst=="echo"){
////some code here
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I am making a shopping list program. For this program, I need to be able to type in a user input that accepts both number (1564, 121,1, etc) and word (hello, goodbye, etc) combinations. The program reads numbers just fine, but it cannot process words. Thank you in advance. The part of the code I am stuck with is below:
int code, option, count = 0;
double quantity, price, cost;
string description;
cin >> code;
while ((code != 123456789) && (count < 2))
{
cout << "Incorrect code, try again \n";
cin >> code;
count++;
if (count == 2)
{
cout << "max # of tries reached. Goodbye. \n";
system("pause");
}
}
Your code variable is now an int. If you wanted that to be a string, declare it so: std::string code;. Note that you might need to #include <string> in the very beginning. Also, if you want to compare it with numbers, either you call something like atoi() (string has .cstr()), or better yet, you might just compare it with "123456789". HTH.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
How could I access a text file and go through word by word. I understand how to open the file but just not how to pull out each word one by one. I think it has something to do with arrays?
Simply:
#include <fstream>
#include <iostream>
int main()
{
std::fstream file("table1.txt");
std::string word;
while (file >> word)
{
// do whatever you want, e.g. print:
std::cout << word << std::endl;
}
file.close();
return 0;
}
word variable will contain every single word from a text file (words should be separated by space in your file).
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a program in c++ where I have to detect spacebar, how can I do? I see that I need the function getch(), but in my program I havenĀ“t conio.h. Exist other solution?
With getchar I need press intro, exist other form that I press only spacebar?
For example, can I introduce a intro without press intro???
Simple Example
#include <iostream>
using namespace std;
int main()
{
char ans;
do
{
//code
}
while(getchar() != 32 || getchar() != ' ');
cout << "Space pressed" << endl;
return 0;
}
Compiled Code
Windows.h:
if(GetAsyncKeyState(VK_SPACE) & 0x80000000)
MessageBox(NULL, "Spacebar pressed!", "TEST", MB_OK);
See no conio.h
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i'm using Linux, i need my program to Extract the HTML source code and put it into a string using C++ language , can you give me a library that can do this
Well the easy solution is:
#include <string>
#include <iostream>
#include <stdio.h>
std::string execu(char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
std::string result = execu("curl http://www.facebook.com");
But this is not considered safe unless you know the string passed is not going to blow anything up.