I'm new to C++ and learning from the C++ Primer book. I would like to know how I would separate statement to print each operand form the following code:
#include <iostream>
int main()
{
std::cout << "Enter two numbers: " << std::endl;
int v1 = 0, v2 = 0;
std::cin >> v1 >> v2;
std::cout << "The numbers " << v1 << " and " << v2 << " multiplied are equal to " << v1 * v2 << std::endl;
}
I think I am meant to simplify the last statement but unsure how. If anyone would be able to help, it would be very helpful for my learning.
Just do std::cout << for each operand.
std::cout << "The numbers ";
std::cout << v1;
std::cout << " and ";
std::cout << v2;
std::cout << " multiplied are equal to "
std::cout << v1 * v2;
std::cout << endl;
Related
I was playing around with pointers and got results I did not expect:
#include <iostream>
#include <vector>
int main() {
int arr[4] = { 1, 2, 3, 4 };
int* pArr = arr;
std::cout << "First number: " << *pArr << " at address: " << pArr;
pArr++;
std::cout << "\nSecond number: " << *pArr << " at address: " << pArr;
pArr++;
std::cout << "\nThird number: " << *pArr << " at address: " << pArr;
pArr++;
std::cout << "\nFourth number: " << *pArr << " at address: " << pArr;
int* pArr2 = arr;
std::cout << "\n"
<< *pArr2++ << "\n"
<< *pArr2++ << "\n"
<< *pArr2++ << "\n"
<< *pArr2++ << "\n";
/*
int* pArr2 = arr;
std::cout << "\n"
<< ++ * pArr2 << "\n"
<< * ++pArr2 << "\n";
*/
}
The two different results:
1 2 3 4 - as expected using the first method
4 3 2 1 - using cout with multiple arguments I do not know the proper name.
So my question is - why does this happen? Using multiple cout statements results in expected for me code, while using just 1 cout results in backwards solution.
As a side note, another confusing thing to me is that pre-increment results in all values being equal. In the commented bit of code, the result is 3 3, no matter the ++ placement with respect to the *.
This code:
std::cout << "\n" << *pArr2++ << "\n";
std::cout << "\n" << *pArr2++ << "\n";
has a well defined order of modifications to pArr and will print
1
2
But this code:
std::cout << "\n" << *pArr2++ << "\n" << *pArr2++ << "\n";
invokes undefined behavior before c++17, because there are multiple modifications to pArr2 that are unsequenced. The program has UB, so it could print anything.
From c++17, there is a sequence point between the modifications, and the above code is guaranteed to print:
1
2
Hi I'm working with GPS output. To be more accurate I'm working using the $GPRMC output. Now the output that I get is in the following form:
$GPRMC,225446,A,4916.45 N,12311.12 W,000.5,054.7,191194,020.3 E,*68"
This output constitutes of time, lats, longs, speed in knots, info about course, date, magnetic variation and mandatory checksum.
The image that I have attached shows the current result I'm getting as I'm taking out the sub strings from the string.
Now I'm getting the time in the hhmmss format. I want it in hh:mm:ss format.
Plus I'm getting the longitude as 4916.45 N. I want to get it as 49 degrees 16' 45".
And the latitude as 123 degrees 11' 12". I'm a beginner so I really don't know how to convert the format. I have also attached my code below.
#include<iostream>
#include<string>
#include<sstream>
#include<stdio.h>
#include<conio.h>
using namespace std;
int main()
{
std::string input = "$GPRMC,225446,A,4916.45 N,12311.12 W,000.5,054.7,191194,020.3 E,*68";
std::istringstream ss(input);
std::string token;
string a[10];
int n = 0;
while (std::getline(ss, token, ','))
{
//std::cout << token << '\n';
a[n] = token;
n++;
}
cout << a[0] << endl << endl;
cout << "Time=" << a[1] << endl << endl;
cout << "Navigation receiver status:" << a[2] << endl << endl;
cout << "Latitude=" << a[3] << endl << endl;
cout << "Longitude=" << a[4] << endl << endl;
cout << "Speed over ground knots:" << a[5] << endl << endl;
cout << "Course made good,True:" << a[6] << endl << endl;
cout << "Date of Fix:" << a[7] << endl << endl;
cout << "Magnetic variation:" << a[8] << endl << endl;
cout << "Mandatory Checksum:" << a[9] << endl << endl;
_getch();
return 0;
}
First thing is that your NMEA sentence is wrong, there should be commas ',' before N and W, so you will actually have to parse "12311.12" and not "12311.12 W". You can check it on this site: http://aprs.gids.nl/nmea/#rmc, you should also always check checksum of sentence - for online checks use: http://www.hhhh.org/wiml/proj/nmeaxor.html.
To parse longitude and latitude I suggest regexps, I am notsaying this is regexp is correct - it only parses data you have provided:
#include <iostream>
#include <string>
#include <regex>
#include <iostream>
std::tuple<int,int,int> parseLonLat(const std::string& s) {
std::regex pattern("(\\d{2,3})(\\d+{2})\\.(\\d+{2})" );
// Matching single string
std::smatch sm;
if (std::regex_match(s, sm, pattern)) {
return std::make_tuple(std::stoi(sm[1]), std::stoi(sm[2]), std::stoi(sm[3]));
}
return std::make_tuple(-1,-1,-1);
}
int main (int argc, char** argv) {
auto loc1 = parseLonLat("4916.45");
std::cout << std::get<0>(loc1) << ", " << std::get<1>(loc1) << ", " << std::get<2>(loc1) << "\n";
// output: 49, 16, 45
auto loc2 = parseLonLat("12311.12");
std::cout << std::get<0>(loc2) << ", " << std::get<1>(loc2) << ", " << std::get<2>(loc2) << "\n";
// output: 123, 11, 12
}
http://coliru.stacked-crooked.com/a/ce6bdb1e551df8b5
You'll have to parse that yourself; there's no GPS parsing in standard C++.
You may want to write your own Angle class in order to have 49 degrees 16' 45" as possible output. You'll want to overload operator<< for that.
i would like to resize my ZZ vector during running program. Is there any way, how to make it? I found methods .setLenght() alternatively .DosetLenght(), but it seems like only initialization step, due to my pro/gram refuses change the vector with these methods..
Many thanks.
Vec<ZZ> v1,v2;
v1.SetLength(8);
v2.SetLength(8);
ZZ velkeCislo,odmocnina,factor,test;
long i = 0;
cin >> velkeCislo;
odmocnina = SqrRoot(velkeCislo);
cout << "new v1 dlzka " << v1.length() << endl;
for(i=0;i<v1.length();i++) {
v1(i) = odmocnina;
odmocnina++;
cout << "Number1 " << v1(i) << endl;
}
for(i=0;i<v1.length();i++){
v2(i)=(v1(i)*v1(i))-velkeCislo;
cout << "Number2 " << v2(i) << endl;
}
bool found=false;
int tp = v1.length();
cout << "old v1 " << v1.length() << endl;
v1.SetLength(tp+1); //causes error
cout << "new v1 " << v1.length() << endl;
The problem with your code is also explained here. You are using the method v1(i) to access the array, but this is a 1-based indexing system so you have out of bounds accesses in your program. Replace v1(i) with v1[i] (which is zero-based) and your program should work.
I'm doing some self study on C++ and have just being doing some chapter on arrays, loops etc. There are a bunch of exercises and the one I'm referencing is quite simple. Initialise two matrices of two rows and three columns.
Output the contents of the matrices (formatted as specified), then perform an addition which is held in a third matrice. Output the third array with the addition done. The code I have works but I'm thinking there's a better way to do the output rather than address each matrice element? I'm thinking of another loop given this is the chapter preceding the exercise, or is this way acceptable?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int amatrix[2][3]=
{
{-5, 2, 8},
{1, 0, 0},
};
int bmatrix[2][3]=
{
{1, 0, 2},
{0, 3, -6},
};
int cmatrix[2][3]=
{
{0, 0, 0},
{0, 0, 0},
};
//add generated matrices
for (int i = 0; i <= 1; i++)
{
for (int j =0; j <= 2; j++)
{
cmatrix[i][j]=amatrix[i][j]+bmatrix[i][j];
}
}
//output to screen - NEED ADVICE FROM HERE
cout << "A= " << endl;
cout << amatrix[0][0] << ", " << amatrix[0][1] << ", " << amatrix[0][2] << endl;
cout << amatrix[1][0] << ", " << amatrix[1][1] << ", " << amatrix[1][2] << endl << endl;
cout << "B= " << endl;
cout << bmatrix[0][0] << ", " << bmatrix[0][1] << ", " << bmatrix[0][2] << endl;
cout << bmatrix[1][0] << ", " << bmatrix[1][1] << ", " << bmatrix[1][2] << endl << endl;
cout << "C= " << endl;
cout << cmatrix[0][0] << ", " << cmatrix[0][1] << ", " << cmatrix[0][2] << endl;
cout << cmatrix[1][0] << ", " << cmatrix[1][1] << ", " << cmatrix[1][2] << endl << endl;
}
cout << amatrix[i][j] in a for loop
I want to input data from txt file.
the file contains 2-d array [5][5]
how can i print out the any value i want?
i don't want to print out the whole 5*5 data
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double distance[5][5] ;
string line;
ifstream ratefile;
ratefile.open("a.txt");
ofstream file;
if (ratefile.is_open())
{
while (! ratefile.eof() )
{
getline (ratefile,line);
ratefile.getline(distance, 25, '*');
cout << "\nDistance [0][0]" << ": " << distance[0][0];
cout << "\nDistance [0][1]" << ": " << distance[0][1];
cout << "\nDistance [0][2]" << ": " << distance[0][2];
cout << "\nDistance [0][3]" << ": " << distance[0][3];
cout << "\nDistance [1][0]" << ": " << distance[1][0];
cout << "\nDistance [1][1]" << ": " << distance[1][1];
cout << "\nDistance [1][2]" << ": " << distance[1][2];
cout << "\nDistance [1][3]" << ": " << distance[1][3];
cout << endl;
cin.get();
return 0;
}
If you only want to output one value and the user should be able to choose a value, you can do something like this:
int x, y;
cin >> x;
cin >> y;
cout << "\nDistance [" << x << "][" << y << "]" << ": " << distance[x][y];
But you should check if the user enter valid numbers (0 <= x < 4 and 0 <= y < 4)
There is part of the code missing, but you are printing values you want. Simply remove the lines you don't want to print.
Of course you can also use variables:
int x = 2,y = 2;
cout << endl << "Distance [" << x << "][" << y << "] : " << distance[x][y];