#include <iostream>
#include <cstdlib>
#include <cstring>
#include <vector>
using namespace std;
class TheNumberGameDivTwo{
private:
public:
string find( int n ){
string n1 = "John";
string n2 = "Brus";
//cout << "Mahesh"<< n2 << n1;
int i,q;
q = 0;
int k = n;
i = n-1;
for ( i = 2 ; i < k ; ++i ){
if ( !(k % i) ){
q += k / i;
k = k % i;
}
}
if ( q % 2 )
return "John";
else
return "Brus";
}
};
int main(){
TheNumberGameDivTwo T;
cout << endl << T.find(6);
cout << endl << "End of Program " << endl;
system("PAUSE");
}
Error 1
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
d:\my documents\visual studio 2012\c++\topcoder\topcoder\source1.cpp 34 1 TopCoder
You are missing #include <string>
Related
I'm using vs2019 and I have a problem the compiler give me an error however I cant solving that
please help me.
the error is : argument of type"const char*" is incompatible with parameter of type"char*"
Blockquote
*** #include <iostream>
#include<cstring>
using namespace std;
class part
{
public:
char partname[30];
int partnumber;
double cost;
void setpart(char pname[], int pn, double c)
{
strcpy_s(partname, pname);
partnumber = pn;
cost = c;
}
void showpart()const
{
cout << "\npartname : " << partname;
cout << "\npartnumber : " << partnumber;
cout << "\ncost($) : " << cost;
}
};
int main()
{
part part1, part2;
part1.setpart("handle bolt", 467, 4500);
part2.setpart("start lever", 549, 2300);
cout << "\nFirst part : "; part1.showpart();
cout << "\nSecond part : "; part2.showpart();
}***
The strings you are giving to setpart are const strings ( const char *) . But set part take a char * as parameter. Since pname will not be modified in your example you can replace void setpart(char pname[], int pn, double c) by void setpart(const char pname[], int pn, double c)
I am trying to solve a very simple task tested by my univeristy's code checker. The code is about a c++ implementation of the equation of the quadratic equation. The code won't work for all cases, I am supplying my code, and if there is some comment, hint, please help me with it.
Code:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
int a, b, c, delta;
double x1, x2, x;
cin >> a >> b >> c;
delta = ((b*b) - (4 * a * c));
if (a == 0) {
if (b != 0) {
x = (-(c*1.0) / (b));
cout << 1 << " " << x;
}
else
cout << "-1";
}
else
if (delta > 0)
{
x1 = ((-(b + sqrt(delta))*1.0) / (2 * a));
x2 = ((-(b - sqrt(delta))*1.0) / (2 * a));
cout << 2 << " " << x1 << " " << x2;
}
else if (delta == 0)
{
x = (-(b*1.0) / (2 * a));
cout << 1 << " " << x;
}
else
cout << "-1";
return 0;
}
I have looked extensively for the problem in this code, but I can't seem to figure out what tragic error I made and why it is triggering a breakpoint.
(After 3 or 4 inputs, it triggers and I don't know why it doesn't trigger at the start or what is causing it)
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout", for example.
string convertDecToBin(int dec)
{
int *arrayHex, arraySize = 0;
arrayHex = new int[];
string s = " ";
int r = dec;
for (int i = 0; r != 0; i++)
{
arrayHex[i] = r % 2;
r = r / 2;
arraySize++;
}
for (int j = 0; j < arraySize; j++)
{
s = s + to_string(arrayHex[arraySize - 1 - j]);
}
delete[] arrayHex;
return s;
}
string convertDecToOct(int dec)
{
int *arrayHex, arraySize = 0;
arrayHex = new int[];
string s = " ";
int r = dec;
for (int i = 0; r != 0; i++)
{
arrayHex[i] = r % 8;
r = r / 8;
arraySize++;
}
for (int j = 0; j < arraySize; j++)
{
s = s + to_string(arrayHex[arraySize - 1 - j]);
}
delete[] arrayHex;
return s;
}
int main()
{
int input = 0;
while (input != -1)
{
cout << "\nEnter a decimal number (-1 to exit loop): ";
cin >> input;
if (input != -1)
{
cout << "Your decimal number in binary expansion: " << convertDecToBin(input);
cout << "\nYour decimal number in octal ecpression: " << convertDecToOct(input);
}
}
cout << "\n\nPress any key to exit. . .";
_getch();
return 0;
}
arrayHex = new int[] is your problem - C\C++ does not support dynamic sizing arrays. You need to specify a size for the array to allocation, otherwise you'll get memory block overruns.
here is my code, I can not figure out why it won't work as a function when the exact code in main() produces the correct answer. The assignment is to convert binary number to decimal.
#include <iostream>
#include <cstdlib>
#include "std_lib_facilities.h"
using namespace std;
int binaryCon(int biNum);
int main()
{
int num, bin, Bnum;
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
Bnum = binaryCon(num);
cout << "The decimal equivalent of " << bin << " : " << Bnum << endl;
}
int binaryCon(int biNum)
{
long dec = 0, rem = 0, base = 1;
enter code here`while (biNum > 0)
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
return biNum;
}
corrected code:
#include <iostream>
#include <cstdlib>
using namespace std;
int binaryCon(int biNum);
int main()
{
int num, bin, Bnum;
cout << "Enter the binary number(1s and 0s) : ";
cin >> num;
bin = num;
Bnum = binaryCon(num);
cout << "The decimal equivalent of " << bin << " : " << Bnum << endl;
getchar();
return 0;
}
int binaryCon(int biNum)
{
long dec = 0, rem = 0, base = 1;
while (biNum > 0){
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
return dec;
}
As you are not using { and } in your while loop may be its going in infinite loop. As its working for this line only
while (biNum > 0)
rem = biNum % 10; // running this line infinite as `biNum > 0`
Use
while (biNum > 0){
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
maybe this?
while (biNum > 0)
{
rem = biNum % 10;
dec = dec + rem * base;
base = base * 2;
biNum = biNum / 10;
}
int main() or int main(int argc, char** argv) must return a value. If you return 0 then it means that there is no problem with code. Another numbers 1,2 etc means there is an error.(Returned numbers are error numbers)
Using Bottom to up DP approach, I am able to solve the problem How to solve http://www.spoj.com/problems/MST1/ upto 10^8.
If input is very large n upto 10^9. I will not be able to create lookup table for upto 10^9. So what will be better approach to solve the problem ?
Is there any heuristic solution ?
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
int main()
{
const int N_MAX = 20000001;
int *DP = new int[N_MAX];
DP[1] = 0;
for (int i = 2; i < N_MAX; i++) {
int minimum = DP[i - 1];
if (i % 3 == 0) minimum = min(minimum, DP[i/3]);
if (i % 2 == 0) minimum = min(minimum, DP[i/2]);
DP[i] = minimum + 1;
}
int T, N; cin >> T;
int c = 1;
while (T--) {
cin >> N;
cout << "Case " << c++ << ": " << DP[N] << endl;
}
delete[] DP;
}