I'm trying to do the following assignment in a c++ book.
After running this:
#include <iostream>
using namespace std;
int main()
{
double first_arg;
double second_arg;
cout << "Enter first argument: ";
cin >> first_arg;
cout << "Enter second argument: ";
cin >> second_arg;
cout << first_arg << " * " << second_arg << " = "
<< cout << first_arg * second_arg << "\n";
cout << first_arg << " + " << second_arg << " = "
<< cout << first_arg + second_arg << "\n";
cout << first_arg << " / " << second_arg << " = "
<< cout << first_arg / second_arg << "\n";
cout << first_arg << " - " << second_arg << " = "
<< cout << first_arg - second_arg << "\n";
I get some unexpected results. Like this result copied straight from the windows cli:
Enter first argument: 7
Enter second argument: 9
7 * 9 = 0x6fcc43c463
7 + 9 = 0x6fcc43c416
7 / 9 = 0x6fcc43c40
7 - 9 = 0x6fcc43c4-2
I'm using the latest version of codeblocks with the default compiler settings. Thanks.
cout << first_arg << " * " << second_arg << " = "
<< cout << first_arg * second_arg << "\n";
You have two cout in one line since there is no semicolon on line 1
To fix this either get rid of the second cout or add a semicolon at the end of the first line on each cout statement.
If you look at the last 2 digits of each answer you will see the answer you wish to get so its still printing out the answer you want its just after the pointer to cout.
Related
I'm printing a statement from classes using getter functions. The top 1.5 lines of my cout statement are not printing. I have tried flushing the stream, and I also copied and pasted the lines that are not printing outside the if statement, and it prints! I can't figure out what is going on. Here is the function:
// display all books out on loan
void displayBorrowed(vector<LibraryBook>& book)
{
cout << "Books currently checked out: " << endl << endl;
for(unsigned int i = 0; i < book.size(); i++)
{
//cout << "ID: " << book[i].getId_() << " Title: "
//<< book[i].getTitle_() << endl << endl;
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
cout << "ID: " << book[i].getId_() << " Title: "
<< book[i].getTitle_() << " Author: "
<< book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_()
<< " Year Published: " << book[i].getYearPubl_() << endl
<< "Due Date: " << book[i].getDueMonth_() << "/"
<< book[i].getDueDay_() << "/" << book[i].getDueYear_()
<< " Date Borrowed: " << book[i].getBorrwdMonth_() << "/"
<< book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_()
<< endl << "Checked out by: " << book[i].getBorrwFirst_()
<< " " << book[i].getBorrwLast_() << endl << endl;
}
}
}
It displays this:
Books currently checked out:
Author: Brendan Behan Year Published: 1958
Due Date: 8/2/2017 Date Borrowed: 7/21/2017
Checked out by: Cassie Peterson
If the lines in the if statement are copied out of the if statement it displays normally:
ID: 78620 Title: Zhuan Falun
I tried changing the if statement to false to display all the books not loaned, and they all displayed the same except for the very last book (number 50 finally displayed the id # and the title. I am at a complete loss. What is going on?
It should look like this:
ID: 78620 Title: Zhuan Falun Author: Brendan Behan Year Published: 1958
Due Date: 8/2/2017 Date Borrowed: 7/21/2017
Checked out by: Cassie Peterson
(havent formatted display yet)
I just changed it to this where I have every single element that doesn't display in its own cout statement, and NONE of it displays!! What??! (up until author, where it started displaying before, I mean.)
for(unsigned int i = 0; i < book.size(); i++)
{
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
cout << "ID: " ;
cout << book[i].getId_();
cout << " Title: ";
cout << book[i].getTitle_();
cout << " Author: "
<< book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_()
<< " Year Published: " << book[i].getYearPubl_() << endl
<< "Due Date: " << book[i].getDueMonth_() << "/"
<< book[i].getDueDay_() << "/" << book[i].getDueYear_()
<< " Date Borrowed: " << book[i].getBorrwdMonth_() << "/"
<< book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_()
<< endl << "Checked out by: " << book[i].getBorrwFirst_()
<< " " << book[i].getBorrwLast_() << endl << endl;
}
It prints when I put an endl at the end of each element:
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
cout << "ID: " << endl;
cout << book[i].getId_() << endl;
cout << " Title: " << endl;
cout << book[i].getTitle_() << endl;
cout << " Author: " << endl;
cout << book[i].getAuthorFirst_() << " " << book[i].getAuthorLast_() << endl;
cout << " Year Published: " << book[i].getYearPubl_() << endl;
cout << "Due Date: " << book[i].getDueMonth_() << "/" << endl;
cout << book[i].getDueDay_() << "/" << book[i].getDueYear_() << endl;
cout << " Date Borrowed: " << book[i].getBorrwdMonth_() << "/" << endl;
cout << book[i].getBorrwdDay_() << "/" << book[i].getBorrwdYear_() << endl;
cout << endl << "Checked out by: " << book[i].getBorrwFirst_() << endl;
cout << " " << book[i].getBorrwLast_() << endl << endl;
}
Books currently checked out:
ID:
47492
Title:
Borstal Boy
Author:
Brendan Behan
Year Published: 1958
Due Date: 8/
2/2017
Date Borrowed: 7/
21/2017
Checked out by: Cassie
Peterson
My suggestion:
Print each of the members in their own line.
Find out which member is problematic.
Dig deeper into the contents of the member to understand how it got there and fix it.
if(book[i].getIsLoaned_() == true)
{
std::cout.flush();
std::cout << "ID: " << book[i].getId_() << std::endl
<< "Title: " << book[i].getTitle_() << std::endl
<< "Author First: " << book[i].getAuthorFirst_() << std::endl
<< "Author Last:" << book[i].getAuthorLast_() << std::endl
<< "Year Published: " << book[i].getYearPubl_() << std::endl
<< "Due Date Month: " << book[i].getDueMonth_() << std::endl
<< "Due Date Day: " << book[i].getDueDay_() << std::endl
<< "Due Date Year: " << book[i].getDueYear_() << std::endl
<< "Borrowed Month: " << book[i].getBorrwdMonth_() << std::endl
<< "Borrowed Day: " << book[i].getBorrwdDay_() << std::endl
<< "Borrowed Year: " book[i].getBorrwdYear_() << std::endl
<< "Checked out by first: " << book[i].getBorrwFirst_() << std::endl
<< "Checked out by last: " << book[i].getBorrwLast_() << std::endl
<< std::endl;
}
}
/* Sometimes "half cout statement not printing problem" may occur due to use of dangling or dangerous pointers. In the code below all cout statements that present, after dangling pointer printing statement are not printing anything.
If we remove the dangling pointer's cout statement then everything will be fine.
code:
// dangling pointer is dangerous because it can hold undesired address. */
#include<bits/stdc++.h>
using namespace std;
int main()
{
int a =5;
char b= 'a';
int *p = &a;
char *p1 =&b;
int *p3; // p3 is dangling pointer
cout<<"size of p "<<sizeof(p)<<endl; // o/p is 8, bcoz x64 bit compiler
gives 8byte and x32 gives 4 byte.
cout<<"size of p1 "<<sizeof(p1)<<endl;
cout<<"size of p3 "<<sizeof(p3)<<endl;
cout<<"address of p3 is "<<&p3<<endl;
cout<< "value at p3 is "<<*p3<<endl; //***dangling or dangerous pointer.***
cout<<"hello my name is rahul ";
cout<<a<<endl;
cout<<&a<<endl;
//cout<<*a<<endl; // shows error (invalid type argument)
cout<<p<<endl; // shows base address
cout<<&p<<endl; // shows pointers address
cout<<*p<<endl;
cout<<"this is an end"<<endl;
return 0;
}
I cannot seem to get the "full"
to display the concatenation of First and last.
It compiles but when I run it it will appear blank.
Can you tell me why?
Have tried to figure it our for hours.
Here are my declerations
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define tax 0.30
#define parking_deductions 10.00
#define overtime_hours 40
#define max_hours 60
#define max_pay 99
string namestring( string first, string last, string full);
I'm attempting to pass this module to my main
string namestring(string first, string last, string full)
{
//input name
cout << "What is your first name? " << endl;
cout << "first name: " << endl;
cin >> first;
cout << "What is your last name? " << endl;
cout << "last name: " << endl;
cin >> last;
//process name
full = last + " " + first;
return full;
}
By calling it like so
namestring(first, last, full );
Where I expect the full name input by the user to be displayed below
cout << left << fixed << " " << "Reg." << " " << " Ovt." << " Hourly" << " Net" << " " << " Gross" << endl;
cout << left << fixed << setprecision(2) << setw(10) << "Name " << " Hours" << " Hours" << " Rate" << " Pay" << " Taxes" << " Deduct" << " Pay" << endl;
cout << left << fixed << setprecision(2) << setw(10) << "====================" << " " << "=====" << " " << "=====" << " " << "=====" << " " << "======" << " " << "======" << " " << " " << "========" << " " << "=======" << endl;
cout << left << setprecision(2) << setw(20) << full << right << " " << right << setw(4) << hours << right << " " << right << overtime << " " << right << pay << " " << right << net_pay << " " << right << taxs << " " << right << parking_deductions << " " << right << gross_pay << right << endl;
cout << endl;
cout << endl;
I'm assuming the goal here is to get the following 3 strings,
First Name
Last Name
Full Name
To do this, you would need to pass the arguments by reference, not by value:
void namestring(string& first, string& last, string& full)
{
//input name
cout << "What is your first name? " << endl;
cout << "first name: " << endl;
cin >> first;
cout << "What is your last name? " << endl;
cout << "last name: " << endl;
cin >> last;
//process name
full = last + " " + first;;
}
There is also no need to return a string if you pass the "full" string by reference, since the function will fill it for you.
string namestring(string first, string last, string& full)
//input name
{
cout << "What is your first name? " << endl;
cout << "first name: " << endl;
cin >> first;
cout << "What is your last name? " << endl;
cout << "last name: " << endl;
cin >> last;
//process name
full = last + " " + first;
return full;
}
You are passing the full by value. So it's the local copy of the function which is modified. You need it to pass by reference.
If you also want the first and last value you also need to pass it by reference.
If anyone has some suggestions - I could utilize the help. The program compiles, but does not
compute properly. I have tried numerous reitterations and I am at an impase. Any suggestions?
I have to initialize all these variables? Thank you....
//This program mimics a calculator
//*****************************************
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double firstint, secint, ansrlt, ansrlt2, ansrlt3, ansrlt4;
char operat;
cout << fixed << showpoint;
cout << setprecision(4);
cout << "This program mimics a standard calculator and outputs a result accurate to 4 decimal
places." <<endl;
cout << '\n';
cout << "Please enter 1st Integer to be calculated: ";
cin >> firstint;
cout << '\n';
cout << "Please enter operation to be performed in the form of *, /, +, - : ";
cin >> operat;
cout << '\n';
cout << "Please enter 2nd Integer to be calculated: ";
cin >> secint;
cout << '\n';
if (operat == '/' && secint == 0)
cout << "Division by 0 not allowed enter again." << endl;
cin.get();
if (operat == '*')
ansrlt = firstint * secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt << endl;
cin.get();
if (operat == '/')
ansrlt2 = firstint / secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt2 << endl;
cin.get();
if (operat == '+')
ansrlt3 = firstint + secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt3 << endl;
cin.get();
ansrlt4 = firstint - secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt4 << endl;
cin.get();
return 0;
}
Firstly, you should explain what you do, what happens, and what you expected to happen - as that will generally make things a lot easier for
anyone reading your question.
However, you're missing {} around the body of your if's everywhere.
e.g. it should be
if (operat == '*') {
ansrlt = firstint * secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt << endl;
}
(And get rid of the cin.get() calls, unless you're using them for debugging purposes)
The user inputs a double and string which gets stored into two arrays. Like so :
{
double DoC;
string Nm;
cout << " Please enter the amount charged to your credit card " << endl;
cin >> DoC;
cout << " Please enter where the charge was made " << endl;
cin >> Nm;
getline(cin, Nm);
cca.doCharge(Nm,DoC);
break;
}
Then the double and string are passed to this :
{
if (amount > 0)
{
for (int i = 9; i != 0; i--)
{
last10withdraws[i] = last10withdraws[i-1];
last10charges[i] = last10charges[i-1];
}
last10withdraws[0] = amount;
last10charges[0] = name;
setBalanceW(amount);
}
else
{
cout << " ERROR. Number must be greater then zero. " << endl;
}
return 0;
}
This seems to work very well for storing the data into the arrays. However I then use this function to display the data inside of the arrays :
{
cout << " Account: Creditcard Withdraws " << " " << " Account: Creditcard Deposits " << " " << " Account: Creditcard last 10 charges " << endl;
cout << " " << last10withdraws[0] << " " << last10deposits[0] << " " << last10charges[0] << endl;
cout << " " << last10withdraws[1] << " " << last10deposits[1] << " " << last10charges[1] << endl;
cout << " " << last10withdraws[2] << " " << last10deposits[2] << " " << last10charges[2] << endl;
cout << " " << last10withdraws[3] << " " << last10deposits[3] << " " << last10charges[3] << endl;
cout << " " << last10withdraws[4] << " " << last10deposits[4] << " " << last10charges[4] << endl;
cout << " " << last10withdraws[5] << " " << last10deposits[5] << " " << last10charges[5] << endl;
cout << " " << last10withdraws[6] << " " << last10deposits[6] << " " << last10charges[6] << endl;
cout << " " << last10withdraws[7] << " " << last10deposits[7] << " " << last10charges[7] << endl;
cout << " " << last10withdraws[8] << " " << last10deposits[8] << " " << last10charges[8] << endl;
cout << " " << last10withdraws[9] << " " << last10deposits[9] << " " << last10charges[9] << endl;
cout << endl;
}
Lets say the user has inputted three doubles into the deposit array. When I call the function to display it I get something like this :
60
30
20
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
-9.25596e+061
How can I make it so that the -9.25596e+061's are 0's? I have not been able to find anything really helping me. Also with the array that contains strings, when it is called to display it displays nothing. Why is this ?
Initialize the array as:
int last10withdraws[10] = {0};
Now all elements are zero.
If the user enters three numbers, then first three elements will be non-zero (assuming only non-zero is allowed), and the rest will be zero.
If last10withdraws is a member of a class m(and you're using C++03), then you can use member-initializer list to default-initialize which will initialize all the elements to zero.
class myclass
{
int last10withdraws[10];
public:
myclass() : last10withdraws()
{ //^^^^^^^^^^^^^^^^^^^ member initializer list
}
};
Hope that helps.
You are getting the error because you are not initializing the values. Start by saying int last10withdraws[10] = {0}; That will initialize all values to zero.
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];