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 2 years ago.
Improve this question
I entered three inputs to the code below and got the results as follows
(1)
input: CTRL+D
result: (blank)
(2)
input: abcCTRL+D
result: (not terminated yet)
(3)
input: abc
result: abc
using namespace::std;
int main()
{
string input;
cin >> input;
cout << input << endl;
return 0;
}
I wonder why I should enter EOF twice to terminate the code in the second case
not just only one time like the first case (it terminated immediately)
Related
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 need to check if all the "B" to the right and the "A" to the left. No matter the position of the "#", as long as all "B" are all on one side and "A" on the other. For example:
-Correct-
BBAA#
BBA#A
BBBB#AAAA
B#BBAAA
#BBAA
-Incorrect-
AAA#BBB
AAB#ABB
B#AAB
BAA#B
#ABAB
BABA#
ABBB#AA
Someone could help me .... I already tried but I could not
thanks
code:
#include <iostream>
#include<regex>
using namespace std;
auto check = [](string &rp)->bool {
regex reg("^(B)*(A)*");
return regex_match(rp, reg);
};
int main()
{
string rp;
do{
system("cls");
cout<<"RP: ";
getline(cin, rp);
}while (!check(rp));
cout<<"\n valid"<< rp<<endl;
return 0;
}
Regex won't move the text for you it will just tell you if the pattern is correct or not. You're going to need another tool alongside regex.
Edit: this regex should work for you. It matches 0 or more #s followed by zero or more Bs 0 or more times followed by 0 or more #s followed by 0 or more As zero or more times. ^ and $ denote the beginning and end of the string.
^(#*B*)*(#*A*)*$
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 very new at this and have an assignment in which I would like for a loop to exit if the user inputs(trans) 'e' but also end if a calculation balance(bal) is less than a constant I have set. Basically as my question states one is a character and the other an integer, will that work? I'm not trying to get people to do my homework for me, so I'm not posting all of my code or assignment, hope it makes sense.
This is the line of code I have
do {
ask user input(&trans)
e or calculation
{
while (trans != 'e'| bal < -OVR);
Just use regular unconditional loop and multiple exit conditions:
while( true ) {
char trans;
std::cin >> trans;
if( !std::cin or trans == 'e' )
break;
calculation;
if( bal > -0VR )
break;
}
So first of all you would not do unnecessary calculations, but what is more important you would make your code more readable and easier to understand - you make loop exit decision where it should be instead of pushing it into the end.
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 4 years ago.
Improve this question
I have an assignment like this:
/*
- Read an input text file (.txt) contain one line to store an array of integer:
Input.txt
4 1 2 -100 -3 10 98 7
- Write SumList function to sum all integer data of the list
- Write a function to find the max of all integer data
- ...
*/
My question is how to count the number of numbers in the txt file to use
/for (int i = 0; i < N; i++), N is number of numbers in file/ for reading the file. Or is there any way else to read this file without initializing N?
Thank you!
Your real question is: how to read a file word by word.
I believe that you've known what file stream is, so here is the code:
fstream file("yourfile.txt", ios::in);
std::string word;
while (file >> word)
{
// convert word to int
}
Now the next question is: how to convert a string to int. I hope you can figure it out on your own --- http://www.cplusplus.com/reference/cstdlib/atoi/
Also, this would be easier: (Thanks to #Fei Xiang)
int i;
while (file >> i)
{
// do something
}
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
How can i print all complete square numbers available in the array
this is my array:
int main()
{
int array[6];
cout<<"Enter 6 #'s: "<<endl;
for(int i=0; i<6; i++) {
cin>>array[i];
}
Here's the algorithm:
For each slot in the array do:
if value in the slot is complete square number, print it.
The difficult part is determining a perfect square.
Hint: use sqrt (square root) function.
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
Increment a alpha numerical string "abcd1234" upto "abcd2000"
output:
abcd1234
abcd1235
.
.
.
abcd2000
Simply:
for (int a = 1234; a <= 2000; ++a)
{
std::cout << "abcd" << a;
}
In C,
for (int i=1234; i<= 2000;i++)
printf("abcd%d\n",i);