'No match for operator=' error in basic code - c++

I'm very new to coding (like one day ago), so you can pretty much assume I know nothing. I'm finding it very hard to debug my extremely simple programs and most of the help I've seen online is either over my head or too specific to the program.
This is the code I'm working on now.
/*
* LogBase8.cpp
*
* Created on: Feb 13, 2015
* Author: Holly
*/
//This program calculates the log base 8 of a number.
#include <iostream>
#include <math.h>
#include <string>
using namespace std;
//Declare variables and constants.
float x, log_8_x;
int main()
{
//Prompt user to input number.
cout << "This program will calculate the log base 8 of a number. Input number to calculate" << endl;
cin >> x;
//Calculate log_8_x.
log_8_x = log(x) / log(8);
//Print log_8_x.
cout << "The log base 8 of " << x << " is " << log_8_x << endl;
//End main.
return (0);
}
I have an error in the log_8_x = log(x) / log(8); line that says
no match for 'operator=' (operand types are 'std::basic_istream<char>::__istream_type {aka std::basic_istream<char>}' and 'double')
I'm using Eclipse, if that's relevant.

You have other errors as stated in your question, but anyway putting a \ to escape the newline appearing in the string literal here
cout << "This program will calculate the log base 8 of a number. Input \
fixes your compilation errors.
See fully working sample here please.

Related

C++ cin prints the variable without me telling it to do that

Introduction
I am trying to code a converter for bin/oct/dec/hex numbers in c++. At first I print a message in a console asking the user to insert the type of conversion he wants to do and, followed by the cin that allows him to enter the conversion and then i ask him for the number followed by the cin that allows him to enter the number.
My problem
My problem is that after the user inserts the conversion, the variable gets printed on the console without me telling it that.
What I've Tried
I looked on the docs and in their example they do it like this:
cout << "Please enter an integer value: ";
cin >> i;
cout << "The value you entered is " << i;
cout << " and its double is " << i*2 << ".\n";
And that is similar to my code(which you will see below).
I also tried to do getline(cin, type) but still the variables would get printed without me coding that.
My code
Faulty code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string type;
int n;
cout << "Insert what type of conversion you want to make(binoct, bindec, binhex, octbin, octdec, octhex, decbin, decoct, dechex, hexbin, hexoct, hexdec): ";
cin >> type;
cout << "Insert the number you want to convert: ";
cin >> n;
return 0;
}
Input
binoct
1001
Output
Insert what type of conversion you want to make(binoct, bindec, binhex,octbin, octdec, octhex, decbin, decoct, dechex,hexbin, hexoct, hexdec):binoct
binoct
Insert the number you want to convert:1001
1001
Expected output:
Insert what type of conversion you want to make(binoct, bindec, binhex,octbin, octdec, octhex, decbin, decoct, dechex,hexbin, hexoct, hexdec):binoct
Insert the number you want to convert:1001
Additional notes
I should mention that before this version of the code I did use cout to print my variables to see if it works but i re-built the code a few times and now there's no cout << type or cout << n inside my code
I looked on stackoverflow and I didn't seem to see anything similar, if this is a duplicate I apologize.
The code is fine, either clean and rebuild your project, or it has something to do with your project / debug settings, that print out the value.
Try Building and Running the Program in release mode.

No match for 'operator*' error

Hello fellow programmers!
I was going to write a small program for calculating total pay for different periods of time depending on the amount of hours and the salary that the user enters. I managed to make a small bit of the program but when I try to run it and test it I get the following error:
Line 33: error: no match for 'operator*' in 'pay * hours_day'
I tried doing a google search on this problem but I am really confused on what causes it.
here is my full program code:
#include <iostream>
#include <random>
#include <time.h>
#include <cstdlib>
#include <string>
using namespace std;
/*
*Program Flowchart*
- Find out how much a worker gets paid per hour, and then find out how many hours they work
a day, and then multiply that to get the total pay in a week or a month or a year.
*/
// global variables
string pay;
float hours_day;
// function that takes the amount of money and calculates the total pay in a week
int total_pay_week() {
cout << "Type in your salary per hour." << endl;
cin >> pay;
cout << "Ok, how many days do you work per day?" << endl;
cin >> hours_day;
float total_day = pay * hours_day;
float total_week = total_day * 7;
cout << "Your total pay in a week is " << total_week << "if you worked " << hours_day << " per day. " << endl;
}
int main() {
total_pay_week();
}
This is a very early version of my program! I just wanted to know what causes this problem.
As #101010 hints at: pay is a string, while hours_day is a float, and while some languages allow you to multiply strings with integers, c++11 (or any other flavor of c) doesn't, much less allow strings and floats to be multiplied together.
I realised that I declared the pay variable as a string right as I was trying to solve this. Thank you to everyone though! It was a very noobish mistake from me.
You declared pay as a string. Change that to int or float and it should multiply. The class std::string has not * operator for this math, so that explains the error text.
You can update your program code as per below to fix the issue
As Scott suggested you are taking input as string and trying to multiply it with float which is not possible. You need to take input as float and everything would work.
#include <iostream>
#include <random>
#include <time.h>
#include <cstdlib>
#include <string>
using namespace std;
/**
* *Program Flowchart*
*
* - Find out how much a worker gets paid per hour, and then find out how many hours they work
* a day, and then multiply that to get the total pay in a week or a month or a year.
*/
// global variables
float pay;
float hours_day;
// function that takes the amount of money and calculates the total pay in a week
int total_pay_week()
{
cout << "Type in your salary per hour." << endl;
cin >> pay;
cout << "Ok, how many days do you work per day?" << endl;
cin >> hours_day;
float total_day = pay * hours_day;
float total_week = total_day * 7;
cout << "Your total pay in a week is $" << total_week << " as you are working " << hours_day << " hours per day. " << endl;
}
int main()
{
total_pay_week();
}
Checkout https://ideone.com/mQj3mh for actual running of the code.

Why do these programs performing the same calculation in a different order give me different output?

Why is the output of these two programs different? I have included the code so you can easily view it and try it. Can anyone explain it to me?
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float pi = 3.14159;
int r;
while (cin >> r) {
cout << "VOLUME = " << fixed << setprecision(3)
<< pi*r*r*r*(4.0/3.0) << endl;
}
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
float pi = 3.14159;
int r;
while (cin >> r) {
cout << "VOLUME = " << fixed << setprecision(3)
<< (4.0/3.0)*r*r*r*pi << endl;
}
return 0;
}
If I input 1523 the first one gives me 14797487059.353, which is the right answer, but the second one gives a negative number.
The 2 screens with the outputs this link include the 2 outputs the first one ir the right the second one is weird !
The code in your screenshot is not what you've posted here. I'd advise that in the future, if you want help with a problem you're having, you post the code you're having problems with - not code that's completely different.
In this case, the problem is simple - r*r*r is too big to fit inside of one int. You can change r to be type long long and this issue should go away. Alternatively, you can use either of the two blocks of code you've posted here, as they both work as you expect.

Beginner C++ - Simple random walk program with a strange setw bug

I've been tasked with creating a very simple random walk program for a C++ class. I wrote it and was fairly sure everything was correct but I am getting this strange bug. From 0 to 10 the steps line up even though the coordinates show that its taking alternating left and right steps.
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{
int steps,i,pos=10;
srand(13699);
cout << "Please enter the amount of steps to be taken: ";
cin >> steps;
cout << endl;
for (i=0;i<=steps;i++)
{
if (rand()%2)
pos+=1;
else
pos-=1;
cout << i << ": " << pos-10 << ": " << setw(pos) << "*" << endl;
}
} // main
There is very obviously some sort of pattern here, but for the life of me I can't figure it out... Here is a link to a screenshot of the output if it helps. http://i.stack.imgur.com/USx4U.png Thanks for any help y'all can give!
The answer is not in the code but rather in your interpretation of the output.
When the pos-10 is less than 0 then the area where you print this value is longer (because of the minus sign), then your 'walker' is shifted right a position in the output.
Similar reason when it goes from 9 to 10 it isn't right.
Think about what it means that the colons on the left are not in a straight line.
The "lining up" for i between 1 and 10 makes sense.
Take the first two lines, for example:
When i == 1, you have pos == 10, and the * is printed 10 spaces after the :.
When i == 2, you have pos == 9, and the * is printed 9 spaces after the :.
But because you are printing 0 (one character) in the first line and -1 (two characters) in the second line, the * end up on the same place in each line.
BTW, you are using the same value (13699) for seeding the RNG every time you run your program.
Try using a more "random" value, for example, a time-based one:
srand((unsigned int)time(NULL));
You'll need to #include <time.h> in your source file.

String.at() returning funky numbers

when I am trying to run this snippet of code I am getting some malfunctions I have not been able to pinpoint the cause of. The first two "cout" lines display the numbers 7 and 3, however, the last "cout" line displays numbers ranging from 50-60 usually (At the moment when I run it I get 55 and 51, which seems to somehow correlate a bit with the numbers I am trying to read). As far as I can tell from some googling and the books I have at hand this should be valid, but there's obviously something I am missing. Thank you for your time.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string text = "73";
int one=0, two=0;
cout << text.at(0) << endl;
cout << text.at(1) << endl;
one = text.at(0);
two = text.at(1);
cout << one << endl << two << endl;
return 0;
}
the program functions correctly: text.at() returns a char, which you implicitly convert to an int. Then you print the value of that int, which are respectively 55 for "7" and 51 for "3" (look here).