Very simple c++ for statement but it wont cout? - c++

My question is simple. I have a 'for' statement in a c++ program and when I compile ignores my cout.
I am using xcode, compiling with xcode and here is my code:
#include <iostream>
using namespace std;
int main ()
{
cout << this prints" << endl;
for(int i=0; i>10; i++)
{
cout << "this doesn't" << endl;
}
return 0;
}
What is the problem?

for(int i=0; i>10; i++)
You initialize i to 0 then only enter the body of the loop if i is greater than 10.
The loop loops as long as the condition i > 10 is true, not until the condition i > 10 is true. This is how all the loops in C++ work: for, while, and do/while.

Your loop condition is backwards. You want it to be i < 10.

You have got the condition for loop incorrect. This should work. Check below:
#include <iostream>
using namespace std;
int main ()
{
cout << "this prints" << endl;
for(int i=0; i<= 10; i++) // ------> Check the change in condition here
{
cout << "this doesn't" << endl;
}
return 0;
}

Related

C++ program not entering a for loop using vector

I was practicing with vector, I wanted to push an element using push_back(); using a for loop, but the program doesn't even enter the for loop, help!!!
#include <iostream>
#include <vector>
using namespace std;
int main()
{
cout << "check ";
int val;
vector<char> vec;
cout << "check " << endl << "Its in the game";
//those two "check" were to confirm if the program is even running and is it a
problem while declaring the vector
for(int i = 0; i < vec.size(); i++){
cout << "enter element for vector" << endl;
cin >> val;
vec.push_back(val);
}
}
Your vector is empty. The for loop starts at zero, which is not less than zero, so the for loop never runs.
Your vector is literally empty, which means vec.size() is 0.So It will never enter the loop. If you know what your vector size is gonna be, you should define it as
std::vector<int> vec(vec_size);
for(int i=0;i<vec_size;++i)
{
//whatever.....
}
Or you could have used a while loop.
while(std::cin>>val)
{
//do your thing....
}

Why does this does not print nor terminate?

I had coaching today and we wrote this little code. What it does is obvious so I wont explain in details. After I run it and type something in, it does not print anything, and does not terminate either. If I switch chars for ints, it works fine. What is wrong with this code?
#include<iostream>
#include<string>
using namespace std;
int main()
{
cout << "wpisz zdanie: ";
string zdanie;
getline(cin, zdanie);
int tab[256];
for(char i=0; i<=255; i++)
{
tab[i] = 0;
}
for(int i=0; i<=zdanie.size()-1; i++)
{
tab[zdanie[i]]++;
}
for(char i=0; i<=255; i++)
{
if(i>='0' && i<='Z')
{
cout << (char)i << " -> " << tab[i] << endl;
}
}
return 0;
}
The issue here is that the char data type has values from -128 to 127, so no matter how much you increment them, they will never reach 255. They will just loop back to -128 after going over 127. You can try using unsigned char or int.

A C++ program that is abour cerr and clog runs differently in Clion and in terminal

I am trying to get use of cerr and clog instead of cout to give out error message. But I met a problem. Below is a simplified version.
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; ++i) {
int x;
cin >> x;
cout << "first" << endl;
cerr << "second" << endl;
}
return 0;
}
But the result was
1
first
second
2
second
first
3
second
first
4
first
second
5
second
first
However, if I use g++ on Win10's cmd to compile and run, it was fine.
1
first
second
2
first
second
3
first
second
4
first
second
5
first
second
So I start to doubt that there are some certain settings that make the Clion inner compile&run environment differ from g++. But I am not sure.
Can anybody tell me why?
There are a lot of things under the hood (SPD mentioned some in the comment, but not all - there is a pty-pipe in the middle). But this just work:
#include <iostream>
using namespace std;
int main() {
for (int i = 0; i < 5; ++i) {
int x;
cin >> x;
cout.flush();
cerr.flush();
cout << "first" << endl;
cout.flush();
cerr << "second"<< endl;
cerr.flush();
}
return 0;
}
Read more here.

Is there a way in C++ to only return the the last instance of a for loop?

Like for example:
#include <iostream>
using namespace std;
int main()
{
for (int n=10; n>0; n--){
cout<< n <<", ";}
}
This will output the numbers 10,9,8,7,6,5,4,3,2,1
So is there a new way so I just get the last instance of the loop, the 1?
I new at this and google isn't giving me any answers.
There is no direct way to detect whether the current iteration of a for loop is the last one. But if the behavior of the loop is predictable, you can usually write code that can detect when you're on the last iteration.
In this case, you could do something like:
if (n == 1) {
cout << n << "\n";
}
in the body of the loop. (Of course it would be simpler in this case to replace the entire loop with cout << "1\n";, but I presume this is an example of something more complex.)
In more complicated cases, you can save whatever information you need in the body of the loop:
int value_to_print:
for ( ... ) {
value_to_print = i;
}
std::cout << value_to_print << "\n";
On each iteration, value_to_print is replaced by the current value of i. The final value is the value of i on the last iteration.
You could create a variable (outside the loop) to hold the "current" value of n; whatever happens to the loop (exit condition reached, break, an exception is thrown...) the value will stay there:
int last_n;
for (int n=10; n>0; n--) {
last_n = n;
cout<< n <<", ";
if (something) {
break; // works in this case
} else if (something else) {
throw some_random_error; // works in this case too
}
}
cout << "The last value of 'n' was " << last_n << endl;
You can use a simple if statement for that.
int main()
{
for (int n=10; n>0; n--) {
cout << n << ", ";
if( n == 1 ) {
return n;
}
}
}
The simplest way to accomplish this is: -
#include <iostream>
using namespace std;
int main()
{
int x;
for (int n = 10; n > 0; n--){
x = n;
}
cout << x;
return 0;
}
I'm new to programming too and was trying to figure out something which will allow me to get the last instance of my loop as output.
I tried something and got the output, see if it can help you (if there's a mistake please let me know).
Here user input string is being replaced by "*" and instead of giving output of every instance i have made so only last instance is given as output.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str;
int string_length;//string length
cout<<"Enter your Email-ID: ";
cin>>str;
string_length = str.length(); //to give the length of input string and use it for the loop
cout<<"lentgh of the string: "<<string_length <<endl;
for(int x = 0; x <= string_length; x++){
str[x] = '*';
while(x==string_length) //string_length is the last instance of the loop
{
cout<<"Here's your Encrypted Email-ID: " <<str<<endl;
break;
}
}
return 0;
}

not getting any output from my letter count program

I'm working on program that counts letters in a piece of text, I can't seem to get it to work. Could someone please point out what I'm doing wrong.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string inputxt;
cout << "enter txt:\n";
getline(cin, inputxt);
char charcheck[ ]={'a', 'b', 'c', 'd'};
int charsize=sizeof(charcheck)/sizeof(char);
int count[charsize];
cout << charsize;
for (int i=0; i==inputxt.length(); i++){
for(int a=0; a==charsize; a++){
if (inputxt[i]==charcheck[a])
++count[a];
}
}
for (int b=0; b==charsize; b++){
cout << "number of " << charcheck[charsize] << ": " << count[charsize];
cout << endl;
}
return 0;
}
Please note I have not put in all the characters to check the text against. Thanks.
for (int i=0; i==inputxt.length(); i++){
The for construct takes 3 parameters:
initialization
continuation condition (and not termination condition like you did). read it as a while ...
loop action (aka. afterthought)
In other words, for (INIT; CONTINUATION; AFTERTHOUGHT) { BODY } is directly translated as:
INIT;
while (CONTINUATION) { BODY; AFTERTHOUGHT; }
Reverse your middle condition, it should be i!=inputxt.length(). The same applies to every other for loops.
In your for loops, you're using == instead of <. For example:
for (int i=0; i==inputxt.length(); i++)
should be:
for (int i=0; i < inputxt.length(); i++)