Shifting Letters in Console C++ - c++

I am trying to shift the cout text from the console down by moving the console cursor back to the start and outputting '\n'. But when I attempt to nothing happens.
#include <windows.h>
#include <iostream>
using namespace std;
int main() {
cout << "Hello!\nThis is a test!" << endl;
SetConsoleCursorPostion(GetStdHandle(STD_OUTPUT_HANDLE), {0, 0});
cout << '\n';
return 0;
}
Is there anyway to move the text down?

The secondary argument of the SetConsoleCursorPostion is the a coordinate for the cursor, so you have to set it like this: SetConsoleCursorPostion(GetStdHandle(STD_OUTPUT_HANDLE), {0, 2});
And by the way, your code does not compile (#includes ???, no namespace for cout, etc.)

First thing first, Please edit your code to correct typos.
second : (0,0) puts the cursor at the beginning not at bottom.
third: cout should be after SetConsoleCursorPosition.
Last : It is recommended to know the console window dimensions or use other functions to get it.
If you want something facilitates console operations for you as a long term solution check ncurses.
I tested with the following code and it works perfect.
#include <windows.h>
#include <iostream>
using namespace std;
int main() {
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {5, 5});
cout << "Hello!" << endl;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), {5, 8});
cout << "This is a test!" << endl;
cout << '\n';
return 0;
}

Related

Using include <string> and using namespace std; but string still breaks my program

I'm following a tutorial for beginner C++ (https://www.udemy.com/course/free-learn-c-tutorial-beginners/learn/lecture/1368442#overview) and they very neatly wrote string text1 = "Hello" and then printed it. I tried this, then again with #include <string> and I'm still getting an error in the console. My code is here:
#include <iostream>
#include <string>
using namespace std;
int main() {
int numberCats = 5; // creates an int variable numberCats with value 5
int numberDogs = 1;
cout << "I have " << numberCats << " cats and " << numberDogs << " dogs." << endl; // prints
cout << "In total I have " << numberCats+numberDogs << " pets." << endl;
numberDogs += 1; // update variable
cout << "I just bought a dog. Now I have " << numberDogs << "." << endl;
string text1 = "Hello!" ; // creates a string variable (class)
cout << text1 << endl;
return 0;
}
And I get this message at the top of my console, and no output:
<terminated> (exit value: -1,073,741,511) udemy_course.exe [C/C++ Application] ...
If I remove the string line and the cout line underneath it, the program runs fine with this message at the top of the console:
<terminated> (exit value: 0) udemy_course.exe [C/C++ Application] ...
And the output:
I have 5 cats and 1 dogs.
In total I have 6 pets.
I just bought a dog. Now I have 2.
I feel like this should be REALLY easy, and can't figure out why it's not working at all. Everything I've looked up says to put #include and/or using namespace std; or change every string to std::string. None of this helps.
I'm using eclipse and minGW, pretty much exactly the same as the udemy course I'm following (except Windows, not Mac). Any help would be much appreciated.

Cout does not work on my other turbo c++ complier

Hello guys i am beginner on the language c++
i was trying to run this code below on my ide"codeblocks" and it works
https://www.youtube.com/watch?v=vLnPwxZdW4Y (link for the tutorial that following )
#include <iostream>
using namespace std;
int main()
{
string charactername = "arnold";
int characterage;
characterage = 10;
cout << "Hello my name is" << charactername<< endl;
cout << "i am " << characterage << endl;
return 0;
}
this code does not work on my other compiler running on dosbox ? any ideas why ?
I suggest you stop using Turbo C++ as it is a very outdated and a discontinued compiler. However, if you don't have the option of using new compilers (I had the same issue as I studied C++ at school), you will have to make the following changes:
using namespace std; cannot be used in Turbo C++. You will have to remove that and replace #include<iostream> with #include<iostream.h>
Data-type string cannot be used in Turbo C++. You will have to declare a character array instead.
You will have to use #include<stdio.h> and the function puts(); to display the character array in case of Turbo C++. Alternatively you can use a loop-statement.
This will be your final code:
#include <iostream.h>
#include <stdio.h>
int main()
{
char charactername[] = "arnold";
int characterage;
characterage = 10;
cout << "Hello my name is ";
puts(charactername);
cout << "i am " << characterage << endl;
return 0;
}
Note: The puts(); function automatically puts the cursor on the next line. So you don't need to use endl;
Or, if you want to use a loop-statement to display the character array
#include <iostream.h>
int main()
{
char charactername[] = "arnold";
int characterage;
characterage = 10;
cout << "Hello my name is ";
int i=0;
while(charactername[i]!='\0') {
cout<<charactername[i];
i++;
}
cout<<endl;
cout << "i am " << characterage << endl;
return 0;
}
'\0' is the last element of the character array. So as long as the loop does not reach the last element, it will print the character array.
a[] = "arnold"; basically means an array is created like this: a[0]='a', a[1]='r', a[2]='n',.... a[5]='d', a[6]='\0'.
Alternatively, if you use cout << charactername; instead of the while loop, it will print the whole name. (This is only in the case of a string variable (character array), for an integer array or any other array you will need the while loop)

Operating on slices of Cube in armadillo

I am trying to get used to armadillo linear algebra library for c++ and I cannot figure out hot to operate on slices(matrices) of a cube. Whenever I try to operate on a slice, the program compiles but does not give any output, not even the outputs of statement before the slice operation.
Here's the code:
#include <armadillo>
#include <iostream>
using namespace arma;
using namespace std;
int main()
{
Cube<double> A(3 , 5 ,1, fill::randu);
Cube<double>B(5,3,1,fill::randu);
Mat<double>x =A.slice(0);
Mat<double>y = B.slice(0);
cout << x << "\n" << y << endl;
cout << x*y << endl; //code works fine if this line is removed
}
the problem is that the code works fine if the last line is removed. Why does this happen? Is there a better way to operate on matrices inside a cube ?
Use directions given in the accepted answer to this question, to install Armadillo on Windows using Visual Studio.
If you have asked the Linker to use blas_win64_MT.lib and lapack_win64_MT.lib libraries, make sure to add the respective .dll's in the same directory as your .exe file. Then using this code, I get the desired output.
#include <armadillo>
#include <iostream>
using namespace std;
using namespace arma;
int main()
{
Cube<double> A(3, 5, 1, fill::randu);
Cube<double> B(5, 3, 1, fill::randu);
Mat<double> x = A.slice(0);
Mat<double> y = B.slice(0);
std::cout << "x:\n" << x << "\ny:\n" << y << std::endl;
std::cout << "x*y:\n" << x*y << std::endl;
}
Output in command window:
Hope that helps!

Coroutines2 - why yield runs when no source called

I am learning how to use boost coroutines2 library. I have read some tutorials and started experimenting with it. But then I found something very confusing. Please take a look at this basic example.
#include <boost/coroutine2/all.hpp>
#include <iostream>
using namespace std;
typedef boost::coroutines2::coroutine<int> cr;
void creator(cr::push_type& yield)
{
cout << "First time." << endl;
yield(1);
cout << "Second time. " << endl;
yield(2);
}
int main()
{
cr::pull_type source{creator};
source();
}
The result is, naturally, this:
First time.
Second time.
But, to my surprise, when I remove the 'source' call in the main function, the result is just the same! (According to tutorials, the coroutine is called first time at construction time, so it is ok that it is called, but should be now called only once!)
#include <boost/coroutine2/all.hpp>
#include <iostream>
using namespace std;
typedef boost::coroutines2::coroutine<int> cr;
void creator(cr::push_type& yield)
{
cout << "First time." << endl;
yield(1);
cout << "Second time. " << endl;
yield(2);
}
int main()
{
cr::pull_type source{creator};
}
The result is still:
First time.
Second time.
When I remove the second 'yield' in the coroutine, the result is also the same:
#include <boost/coroutine2/all.hpp>
#include <iostream>
using namespace std;
typedef boost::coroutines2::coroutine<int> cr;
void creator(cr::push_type& yield)
{
cout << "First time." << endl;
yield(1);
cout << "Second time. " << endl;
}
int main()
{
cr::pull_type source{creator};
}
Result:
First time.
Second time.
How is that possible? How does it work? I expected that when I don't call the coroutine, then even if there is another 'yield' waiting, nothing will happen.
And I find also strange this behaviour:
When I add another 'source' statements in the main, the code still prints the same, as at the beginning!
#include <boost/coroutine2/all.hpp>
#include <iostream>
using namespace std;
typedef boost::coroutines2::coroutine<int> cr;
void creator(cr::push_type& yield)
{
cout << "First time." << endl;
yield(1);
cout << "Second time. " << endl;
yield(2);
}
int main()
{
cr::pull_type source{creator};
source();
source();
}
Result:
First time.
Second time.
No error, even when sourcing more times than there are 'yield's.
Only after adding one more 'source' in the main function do I receive a runtime error (This application has requested the Runtime to terminate it in an unusual way...)
int main()
{
cr::pull_type source{creator};
source();
source();
source();
}
Could someone help me please with understanding this behaviour?
The reason lies in a bug in Boost. I checked that in Boost 1.65.1 everything works fine. Here is the proof: https://wandbox.org/permlink/pRuSgnwa3VPdqNUk

Does it make sense to apply make_move_iterator together with vector::insert ?(C++)

I have a vector of vectors, and I want to connect them one by one to form a long vector. This could be done by inserting at the end. Inspired by this question, I was thinking that using make_move_iterator would replace copy with move and thus would be more efficient. But the following test demonstrates that make_move_iterator will cause a larger time consumption.
#include <iostream>
#include <string>
#include <vector>
#include <chrono>
using namespace std;
int main()
{
string a = "veryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryveryverylongstring";
vector<string> b(10,a);
vector<vector<string> > c(1000,b);
vector<string> d,e;
auto t1 = chrono::system_clock::now();
for(auto& item : c)
{
d.insert(d.end(),item.begin(),item.end());
}
cout << c[0][0].length() << endl;
auto t2 = chrono::system_clock::now();
for(auto& item:c)
{
e.insert(e.end(), std::make_move_iterator(item.begin()),std::make_move_iterator(item.end()));
}
auto t3 = chrono::system_clock::now();
cout << chrono::duration_cast<chrono::nanoseconds>(t2-t1).count() << endl;
cout << chrono::duration_cast<chrono::nanoseconds>(t3-t2).count() << endl;
cout << c[0][0].length() << endl;
cout << "To check that c has been moved from." <<endl;
}
//Output:
//122
//1212000
//1630000
//0
//To check that c has been moved from.
Thus I'm wondering, does this approach really help improve efficiency?
The test in the question description was conducted on cpp shell
I later tried on ideone and it turned out that make_move_iterator is obviously more efficient. So it seems to be a compiler-dependent thing.
122
320576
98434
0
To check that c has been moved from.