Using cout << endl; between functions fixes code [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I have simplified the code to get rid of unrelated objects. This is my code:
#include <iostream>
#include <fstream>
using namespace std;
fstream asdf;
int input;
void import_image(){
asdf.seekg(0);
char character;
for(int k = 0; k < 40; k++){
asdf.get(character);
input = (unsigned int)(unsigned char)character;
}
}
void print_hello_world(){
for(int rows; rows <= 27; rows++){
cout << "hello world" << endl;
}
cout << "goodbye.";
}
int main(){
asdf.open("abc.txt", ios::binary | ios::in);
cout << asdf.is_open() << endl;
import_image();
//cout << endl;
print_hello_world();
return 0;
}
Running this code results only in
1
goodbye.
--------------------------------
Process exited after 0.1511 seconds with return value 0
however removing double slash (simply adding cout << endl;) fixes everything. I have no idea why it happens and would like to now why is it so. I know that variable "rows" has no value, but why does printing a new line fix everything?

The new "endl"
is a great sign
that what you see,
is called "UB".
Your program has Undefined Behavior (UB) because your int rows that you use for the loop iterations is uninitialized.
By UB definition anything may happen. Activate all (sane) compiler warnings to find errors like this earlier in your development process.
Undefined behavior yield working programs by completely random changes (for example the addition of std::endl) but in the end it's undefined behavior.

Related

C++ Can't use vector and string literal in cout [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm a student who just learned to use C++ for a few weeks. I write C++ code on Code:: Block and I am trying to run this code to learn about vector:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
vector<int> intVector;
for(int i = 0; i < 10; i++)
{
intVector.push_back(i+1);
}
cout << "Numbers in vector: ";
for(int i = 0; i < intVector.size(); i++)
{
cout << intVector[i] + " ";
}
}
But the output is really weird:
Numbers in vector: vector::_M_emplace_back_auxector::_M_emplace_back_auxctor::_M_emplace_back_auxtor::_M_emplace_back_auxor::_M_emplace_back_auxr::_M_emplace_back_aux::_M_emplace_back_aux:_M_emplace_back_aux_M_emplace_back_aux
Process returned 0 (0x0) execution time : 0.047 s
Press any key to continue.
Does anyone know how to fix this problem? Do I use vector in the wrong way?
Try changing the cout line like this:
cout << intVector[i] << " ";
What you're trying to do is add an int& and a string literal -- in some compilers it will emit a warning indicating for you that you're not getting what you intended for.
For example with Clang:
warning: adding '__gnu_cxx::__alloc_traits>::value_type' (aka 'int') to a string does not append to the string [-Wstring-plus-int]
cout << intVector[i] + " ";
~~~~~~~~~~~~~^~~~~

for loop result 0 in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I am a newbie in C++, I started to learn coding in C++ two weeks ago. Why does my code below always give me result 0 when I build and run? Please help
# include <iostream>
# include <string>
using namespace std;
int main ()
{
int input = 1;
cout << "input your number : \n";
cin >> input;
int faktorial = 1;
for(int i=1;i<=input;i++)
{
faktorial = faktorial * i;
}
cout << "factorial value from number " << input << " is " << faktorial << endl;
}
Your code works: https://ideone.com/CYFaxo
I suspect your problem is, you are looking at program exit code. When you don't return any value from main, program exit code is 0 (this is special case, and only non-void function where you may leave the return statement out), which conventionally means success (non-zero exit code usually indicates some kind of error, by convention).
Try to find the program output from your IDE, it should have the correct printout.

From the user's input total the odd and even numbers. input numbers in between 0 to 99 [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
#include <iostream>
using namespace std;
int main()
{
int i,t,x[20], even, odd, prime;
cout << "Enter 20 integer numbers from 0 to 99: "<<endl;
for (i=1;i<=20;i++)
{
cout << "Input " << i <<":";
cin >> x[i];
}
cout << "\nPrime numbers are: " << endl ;
prime=1;
for (i=2; i<=20 ; i++)
{
for(t=2;t<x[i];t++)
{
if(x[i]%t==0)
{
prime=0;
}
}
if(prime==1)
{
cout << x[i] << endl;
}
prime=1;
}
for(i=1; i<=20; i++) // this is where i have problem.
{
if(x[i]% 2 == 0)
{
even++;
}
else
{
odd++;
}
}
cout << "Number of odd numbers: " << odd << "\n";
cout << "Number of even numbers: " << even << "\n";
return 0 ;
}
When i compile it shows even (40) and odd (10) for input of 0 till 19. Where it should show even 10(including the 0) and odd (10). Im not sure where am i doing it wrongly. I hope someone can help me improve the code.
Variables even and odd are never set to a known value, so you are not formally allowed to read from them. Doing so invokes that most infamous Standardese concept: undefined behaviour. So the values of these variables could be right or could be wrong; the variables and all code trying to read them could be optimised entirely out of your program; or anything can happen. You cannot rely on these variables doing anything right. All attempts to read them make your program ill-formed, so now it can do anything, including things you would never have imagined.
You should search for the abundant background info about these concepts, but I like to think I made a fairly decent summary here: https://stackoverflow.com/a/38150162/2757035
Also, as Thomas points out in the comments, you appear not to understand how array indexing works: Indexes are 0-based. So, int i[20] declares 20 elements numbered from 0 to 19. You try to access index 20, which is not part of the array and hence is more undefined behaviour.

Why doesn't my c++ code work? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
What have I done wrong with the following code?
#include <iostream>
using namespace std;
main ()
{
int a;
int b;
int sum;
cout << "Enter first number \n";
cin >> a;
cout << "Enter second number \n";
cin >> b;
sum = a+b;
cout << "The sum of both numbers is" << sum << endl;
return 0;
}
Does the editor you are using tells errors, so the code is not executing? Or som exception rises? Or it is executing but nothing is shown? Please specify your problem accurately.
Anyway, you must use
int main ()
instead of
main()
Notice that your code returns a value. The last line of you code is:
return 0;
Thus, you must specify an int return type.
Check your initial lines with this.
#include <iostream>
using namespace std;
int main ()
{

why am I getting random results when incrementing an int [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have to write a function that goes through a string, finds any capital letters present and makes them lowercase. I decided to add a bit of code to show what letters were found and how many were found. Although on each compilation, the value of 'cnt' yields ridiculous results.
#include <iostream>
#include <vector>
using namespace std;
int upper(string s) {
int cnt;
vector<char> v{};
for (auto& i : s) {
if (isupper(i)) {
v.push_back(i);
i = tolower(i);
++cnt;
}
}
cout << "new string is '" << s << "'\n"
<< "number of capitals found is " << cnt << "\n"
<< "letters found were ";
for (auto l : v)
cout << l << " ";
return 0;
}
int main() {
string l = "This IS a TeSt";
upper(l);
}
I'm sure I must have done something wrong with the loop but whatever the problem is, I cannot find it.
The variable cnt is never initialized when used, change
int cnt;
to
int cnt = 0;
You failed to initialize the local variable cnt. Using an uninitialized value provokes undefined behavior, under which basically anything can happen.
Use int cnt=0; and please turn on all your compiler warnings.