cstring << no operator found which takes a right-handed operand of type 'std::string' - c-strings

I have an assignment in which we are supposed to use cstrings instead of strings. The problem lies in the << operands in three out of the five cout statements. Can anybody help me figure out how to get the operand errors to go away? The code is posted below. The error locations are listed in bold! Thanks!
#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
int strLength, wPointer;
string custNumber, year, workOrderNumber;
string workOrder = "91800w940770";
strLength = workOrder.length();
wPointer = workOrder.find('w', 0);
custNumber.assign(workOrder, 0, wPointer);
year.assign(workOrder, wPointer + 1, 2);
workOrderNumber.assign(workOrder, wPointer + 3, strLength);
cout << "The length is " << strLength << endl;
cout << "The location of the w is " << wPointer << endl;
std::cout << "The customer number is " **<<** custNumber << std::endl;
cout << "The year of the order is " **<<** year << endl;
cout << "The order number is " **<<** workOrderNumber << endl;
system("pause");
return 0;
}

custNumber, year and workOrderNumber are strings.
Include <string> to use them with << operator.

The library "<<" function won't work on std::strings. You could create your own function that would output the characters of the string one char at a time

Related

how the use of cin.ignore and getline should be

#include <iostream>
using namespace std;
int main () {
int pemboking, kode_lap[8], durasi[8];
string nama[8], tanggal[8], jam[8];
cout << "\nMasukan jumlah pembooking : ";
cin >> pemboking;
cout << endl;
for (int i = 0; i < pemboking; i++) {
cout << "Transaksi ke " << i + 1 << endl;
cout << "Masukan Nama : ";
getline(cin, nama[i]);
cout << "Masukan Kode Lapangan : ";
cin >> kode_lap[i];
cout << "Masukan Tanggal Sewa : ";
cin >> tanggal[i];
cout << "Masukan Jam main : ";
getline(cin, jam[i]);
cout << "Masukan Durasi Jam Main : ";
cin >> durasi[i];
cout << endl;
cout << endl;
}
}
when I use cin.ignore, the error will go directly to the input with index 1, index 0 is missed. if you don't use cin ignore it just skips the input using getline.
so how should i use the code, for a space input from the user.
although not in the same way, I hope the code meets my expectation that it can ask the user to input spaces for string type

how do i pick a memory in an array that i create after displaying the array value. how to slect thte value with cin when the program run

string bookname [5] = {"STPM BOOKS","SPM BOOKS","PMR BOOKS","TEXT BOOK","PRACTICAL BOOK"};
float price [5] = {30.90,24.90,19.90,45.90,15.90};
for(int i = 0; i < 5 ; i++)
{
cout << i+1 << "book name " << bookname[i] << " ---- RM " << price[i] << endl;}
i want to select the value for example like stpm books and its price from the array and display it to cout after selecting from a cin and doing the selection when the code was excercuted
}
Based on your updated query, asking for an exit condition, I have modified the code. Please note that I have used getline(cin, answer) to handle the input. I use getline() because I am less likely to mess up when I use getline() over cin >> answer.
Note that getline(cin, answer) does a few things for you:
Returns true if input was received
Fills in the answer
Gets an entire line, until user provides new line
Thus, the line while (std::getline(std::cin, answer) && answer != "n") will get an entire book, even if there are spaces. The && allows for a check of the answer for "n" before entering the while loop's body.
As answer is a std::string, the comparison is to "n", not 'n'.
#include <iostream>
#include <string>
#include <iomanip> // setprecision
int main()
{
std::string bookname[5] =
{"STPM BOOKS", "SPM BOOKS", "PMR BOOKS", "TEXT BOOK", "PRACTICAL BOOK"};
float price[5] = {30.90, 24.90, 19.90, 45.90, 15.90};
std::string answer;
float cost;
std::cout << "Please provide the name of a book or type 'n' to exit"
<< std::endl;
// get one line into answer, and also exit while if answer is "n"
while (std::getline(std::cin, answer) && answer != "n") {
bool found = false;
for (int i = 0; i < 5; i++) {
if (bookname[i] == answer) {
std::cout << "The book " << bookname[i] << " costs $" << std::fixed
<< std::setprecision(2) << price[i] << std::endl;
found = true;
break; // /no need to continue inside the for loop
}
}
// on last loop, give up
if (!found) {
std::cout << "The book " << answer << " is not in our inventory"
<< std::endl;
}
}
}

C++ How to Increment Int Variable From String Array Element

I'm wondering if there is a way to increment an int variable when a specific string array element is called? I'm working on a project for fun and have run into a roadblock. The goal of my project is to have a program that determines what genres of electronic music the user likes. The program will present two DJ's and ask the user which one they prefer or allow them to select neither. Each DJ will have up to three genres that they mainly specialize in, and I have created int variables for each (with all being set to 0). Once a user selects a DJ, I want points to be assigned to each genre variable in which the DJ is associated with. I'm unsure of how to set up this rule as everything I have attempted so far has failed (two example attempts are commented out in the code). Eventually my plan is to develop logic to have the DJ's randomly selected, however I need to have the genre tally allocation set up first. Is there any way this can be done? Any help would be greatly appreciated. Cheers!
###include "stdafx.h"
###include < iostream>
###include < iomanip>
###include < string>
###include < array>
###using namespace std;
int main()
{
cout << "Hello! This program is designed to figure out what Electronic Music you like based on artists presented and the answers you choose...\n" << endl;
cout << "When you are ready to begin press \"Enter\"..." << endl;
getchar();
int bigRoom = 0;
int deepHouse = 0;
int drumBass = 0;
int dubstep = 0;
int electroHouse = 0;
int futureHouse = 0;
int hardDance = 0;
int house = 0;
int progressiveHouse = 0;
int techno = 0;
int trance = 0;
int trap = 0;
string textArray[5]{ "DeadMau5", "Armin Van Buuren", "Avicii", "Ferry Corsten", "Kaskade"};
string answer;
cout << "Select the DJ you prefer by number. Otherwise select 3 if you don't know them. " << endl; //Haven't coded option 3 yet.
cout << "1 - " << textArray[1] << endl;
cout << "2 - " << textArray[2] << endl;
cin >> answer;
/*
if (textArray[1]) {
++trance;
}
for (textArray[1]) {
++trance;
}
*/
if (answer == "1") {
cout << "You have selected: " << textArray[1] << endl;
}
else if (answer == "2") {
cout << "You have selected: " << textArray[2] << endl;
}
//cout << trance << endl;
}
You can increment each tally once the user has selected a DJ:
if (answer == "1") {
cout << "You have selected: " << textArray[1] << endl;
++trance;
// ++ ohter genres you want to increment
}
else if (answer == "2") {
cout << "You have selected: " << textArray[2] << endl;
++trance;
// ++ ohter genres you want to increment
}

CUSP library called from Fortran not working

I want to repetitively solve the CG/BicGSTAB using CUSP solver, called from Fortran. To avoid transfers I am passing the Fortran data directly to CUSP. The code compiles but breaks at the run time flagging:
terminate called after throwing an instance of 'thrust::system::system_error'
what(): invalid argument
terminate called recursively
Aborted (core dumped)
Let alone the core of the code, even the print stream is not happening. The code of course is in the preliminary stage, but I wonder what is wrong with it.
extern "C" void bicgstab_(int *device_I, int *device_J, float *device_V, float *device_x, float *device_b, int *n, int *nnz){
int N = *n;
int NNZ = *nnz;
std::cout << N << " " << NNZ << " " << *device_I << std::endl;
for(int i=0; i<N;i++)std::cout << device_I[i] << " "; std::cout << std::endl;
for(int i=0; i<NNZ;i++)std::cout << device_J[i] << " "; std::cout << std::endl;
for(int i=0; i<NNZ;i++)std::cout << device_V[i] << " "; std::cout << std::endl;
for(int i=0; i<N;i++)std::cout << device_x[i] << " "; std::cout << std::endl;
for(int i=0; i<N;i++)std::cout << device_b[i] << " "; std::cout << std::endl;
// *NOTE* raw pointers must be wrapped with thrust::device_ptr!
thrust::device_ptr<int> wrapped_device_I(device_I);
thrust::device_ptr<int> wrapped_device_J(device_J);
thrust::device_ptr<float> wrapped_device_V(device_V);
thrust::device_ptr<float> wrapped_device_x(device_x);
thrust::device_ptr<float> wrapped_device_b(device_b);
// use array1d_view to wrap the individual arrays
typedef typename cusp::array1d_view< thrust::device_ptr<int> > DeviceIndexArrayView;
typedef typename cusp::array1d_view< thrust::device_ptr<float> > DeviceValueArrayView;
std::cout << wrapped_device_I[3];
/*
DeviceIndexArrayView row_indices (wrapped_device_I, wrapped_device_I + (N+1));
DeviceIndexArrayView column_indices(wrapped_device_J, wrapped_device_J + NNZ);
DeviceValueArrayView values (wrapped_device_V, wrapped_device_V + NNZ);
DeviceValueArrayView x (wrapped_device_x, wrapped_device_x + N);
DeviceValueArrayView b (wrapped_device_b, wrapped_device_b + N);
// std::cout << device_x[0] ;
// for(int i=0;i<NNZ;i++)std::cout << column_indices[i] << std::endl;
// combine the three array1d_views into a csr_matrix_view
typedef cusp::csr_matrix_view<DeviceIndexArrayView,
DeviceIndexArrayView,
DeviceValueArrayView> DeviceView;
// construct a csr_matrix_view from the array1d_views
DeviceView A(N, N, NNZ, row_indices, column_indices, values);
// set stopping criteria: // iteration_limit = 100 // relative_tolerance = 1e-5
cusp::verbose_monitor<float> monitor(b, 100, 1e-5);
// solve the linear system A * x = b with the Conjugate Gradient method
// cusp::krylov::bicgstab(A, x, b);*/
}
If this is not feasible, I can move over to another approach,but as I am not sure about the correctness, I am unable to decide. Any help is appreciated.

Output file to .txt and console c++

I have been trying to figure out how to mirror the programs output to a .txt file as well as the console. I am relatively new to this, so I'm lost.. Please help, here is what I have so far.
#include <iostream>
#include <fstream>
#include <cmath>
#include <cstdlib>
using namespace std;
bool isPrime (int);
int main ()
{
int numbers;
ifstream inputFile;
//open the file
inputFile.open("22.txt");
//output to file
ofstream outFile;
outFile.open("PrimeNumbers.txt");
while(inputFile >> numbers)
if(isPrime(numbers))
outFile << numbers << endl;
cout << numbers << " is a prime number." << endl;
} //end of main function
//function formula
bool isPrime (int num)
{
int count=0;
for (int numb=2;
numb<=num/2;
numb++)
{
if(num%numb==0)
count++;
}
if (count>0)
return false;
else
return true;
}
One error that stands out to me is:
while(inputFile >> numbers)
if(isPrime(numbers))
outFile << numbers << endl;
cout << numbers << " is a prime number." << endl;
You don't have any blocks (defined by curly brackets) so this snippet is equivalent to:
while(inputFile >> numbers)
{
if(isPrime(numbers))
{
outFile << numbers << endl;
}
cout << numbers << " is a prime number." << endl;
}
See how the "cout" statement isn't affected by the condition? What you wanted to write is:
while(inputFile >> numbers)
{
if(isPrime(numbers))
{
outFile << numbers << endl;
cout << numbers << " is a prime number." << endl;
}
}
(Also, see my comment underneath your question requesting a more precise definition of the problem if you want more help)

Resources