Sep 24, 2010 at 4:55pm UTC
Something not right with the results this program gives whats wrong?
#include <iostream>
#include <cmath>
#include <fstream>
int main()
{
using namespace std;
float x;
float y;
float o;
cout << "Type metters - Min." << endl;
cin >> x;
cin >> y;
y = y*60;
o = (x /y)*3,6;
cout << "Your speed was ---->" << o << "km/h" << endl;
system("pause");
return 0;
}
Sep 24, 2010 at 4:59pm UTC
In C++, a decimal point is used, so it should be 3.6
The comma operator takes an arbitrary number of expressions and evaluates to the last one (6 in this case).
Sep 24, 2010 at 5:14pm UTC
Oh yes i just saw that thanks! but i have another question as well i fixed this program below and in every case the response is take pill today! and its supposed to give a pill every 2 days
#include <iostream>
using namespace std;
int main ()
{
cout << "Enter day and month" <<endl;
int d1 , d2 , m;
cin >> d1, d2, m;
if (m== 01 || 04 || 05 || 010 || 11 || 12 && d2 == 1 || 3 || 5 || 7 || 011 )
{
cout << "Take pill today./n ";
}
else if (m==01 || 04 || 05 || 010 || 11 || 12 && d2 != 1 || 3 || 5 || 7 || 011)
{
cout << "Dont take pill./n";
}
else if (m==2 || 3 || 6 || 7 || 011 || 10 && d2== 2 || 4 || 6 || 010 || 0)
{
cout << "Take pill today ! /n";
}
else
{
cout << "Dont take pill today. /n";
}
system("pause");
return 0;
}
Sep 24, 2010 at 5:25pm UTC
if (m== 01 || 04 || 05 || 010 || 11 || 12 && d2 == 1 || 3 || 5 || 7 || 011 )
You can't use || or && like this, you'll need to do this instead:
if ((m == 01 || m == 04 || /*...*/ ) && (d2 == 1 || d2 == 3 || /*...*/ ) )
Sep 24, 2010 at 6:19pm UTC
ok fixed what you told me now it only gives the other answer every time :P Don't take pill
#include <iostream>
using namespace std;
int main ()
{
cout << "Enter day and month" <<endl;
int d1 , d2 , m;
cin >> d1, d2, m;
if ( (m == 01 || m == 04 || m == 05 || m == 010 || m == 11 || m == 12 ) && ( d2 == 1 || d2 == 3 || d2 == 5 || d2 == 7 || d2 == 011) )
{
cout << "Take pill today./n ";
}
else if ( (m == 01 || m == 04 || m == 05 || m == 010 || m == 11 || m == 12 ) && ( d2 != 1 || d2 != 3 || d2 != 5 || d2 != 7 || d2 != 011) )
{
cout << "Dont take pill./n";
}
else if ( ( m == 02 || m == 03 || m == 06 || m == 07 || m == 011 || m == 10 ) && ( d2 == 2 || d2 == 4 || d2 == 6 || d2 == 010 || d2 == 0) )
{
cout << "Take pill today ! /n";
}
else
{
cout << "Dont take pill today. /n";
}
system("pause");
return 0;
}
Sep 24, 2010 at 6:31pm UTC
Don't start your numbers with 0. C++ takes that as an indication you are using octal. Also, your cin>> statement is wrong; the correct way to read in the variables that way is to do:
cin>>d1>>d2>>m;
Sep 24, 2010 at 6:34pm UTC
Because 010 does not equal 10, and like wise, 011 does not equal 11.
010 equals 8, because it is read as 10 octal .
011 equals 9, because it is read as 11 octal .
Remove the suffixing 0 and it might work, not tested, though.
EDIT:
Zhuge beat me too it, blast! ;)
Last edited on Sep 24, 2010 at 6:34pm UTC
Sep 24, 2010 at 11:38pm UTC
Also, the newline character is \n and not /n
Sep 25, 2010 at 1:13am UTC
The program below has the follow error : [Linker error] undentifined reference to 'populate(double*,int)'
anyone can help?
#include <cmath>
#include <iostream>
#include <fstream>
using namespace std;
int main (){
int populate(double array[],int y);
int y;
double persons[10]={};
populate(persons,10);
double x;
int i;
for (i=0;i<y;i++){
cout<<"Give me the number of cakes for person number:" << (i+1) << "\n";
cin>>x;
persons[i]=x;
}
return i;
system("pause");
return 0;
}
Sep 25, 2010 at 3:08am UTC
You didn't define the populate function, you just declared it. For it to work you need to define it somewhere.
Sep 25, 2010 at 2:55pm UTC
I define a function after my main program using void command? and can you guys give me some tips what to practice with in c++ so i can progress to more complicated things soon!