ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
learning C++ , started in win on Dev cpp, and lost my comp for a while (tech difficalties ) got rid of the dual boot and now am on 12.04 lts, never compiled in linux and its frustrating hehe, i tried both c++ and g++
before i stoped i had made a small converting (non gui) app and it ran grt before (in a terminal) but .... well its going something like this
:~/cp$ c++ main.cpp -o main
main.cpp: In function ‘int main()’:
main.cpp:143:17: error: ‘system’ was not declared in this scope
main.cpp: At global scope:
main.cpp:308:1: error: ‘endl’ does not name a type
am lost on both errors *** i din't even know endl had types
this is the code
Code:
# include <iostream>
using namespace std;
int temps();
int heavy();
int main ()
{
float a;
char b;
float c;
float d;
float e;
float f;
float g;
float h;
float i;
float j;
float k;
float l;
int u =1;
int n;
cout << "################################################################################"<< endl
<<">WELCOME TO THE C,M.D CONVERTING TOOL >>>>><<<<<<<>>>>>>>><<<<<<>>>><<<<>>><<<>>>><<<<<<>>>><<<<<<<>>>>><<<<<>>>><<<<<>>><<<>>>>><<<<<>>>><<<>>><<<>>><<><<<>>><"<< endl
<<"--------------------------------------------------------------------------------" <<
endl << "ENTER 1 TO USE THE DIST CONVERTER >><< ENTER 2 TO USE THE TEMP CONVERTER" << endl <<"ENTER 3 TO USE WEIGHT CONVERTER, ENTER ANY OTHER # TO EXIT" <<
endl <<"********************************************************************************"
<< endl
<<"********************************************************************************" << endl
<<"********************************************************************************" << endl
<<" ******* ****** ** ** *** *** ** **" << endl
<<"********* ********** ** ** *** ** ** **** **" << endl
<<"*** ********* **** ** ** ** ** ***** ** ** **" << endl
<<"*** ****** **** ** ** ****** ***** ** ** **" << endl
<<"*** **** **** ***** **** ** ** ** ****" << endl
<<"--------------------------------------------------------------------------------" << endl
<<"--------------------------------------------------------------------------------" << endl
<<">CHOSE A CONVERTER<<<<<<<<<<<>>>>>>>>>>>><<<<<<<<<<>>>>>>><<<<>>><<<>>>><<<<<>><" << endl;
cin >> (n);
if (n <= 3 && n >=1)
{
if ( n == 3)
heavy();
else if (n==2)
temps();
if (n == 1)
while (u == 1)
{
cout << "---------------------------------------" <<
endl << "WELCOME TO THE C.M.D DIST CONVERTER" <<
endl << "***************************************" <<
endl << "* miles to km.> k" <<
endl << "*km to miles >> m" <<
endl << "*yard to metre> M ********************************************************" <<
endl << "* metre to yard>y ** * * * * * * * * * **" <<
endl << "*foot to metre> t ** * * * * * * * * * **" <<
endl << "*metre to foot> T ********************************************************" <<
endl <<"*foot to cm > f " <<
endl <<"*cm to foot > c" <<
endl <<"* inch to cm > i " <<
endl <<"*cm to inch>c > n" <<
endl << "-----------------"<<
endl <<"ENTER FUNCTION KEY " <<
endl <<"-------------------" <<
endl;
cin >>(b);
cout <<" ENTER THE DISTANCE" << endl
<< "___________________ " << endl;
cin >> (a);
(c) = (a)*1.609;
(d) = (a)/.5936133;
(e) = (a)/1.09;
(f) = (a)*1.09;
(g) = (a)/0.3028;
(h) = (a)*30.48;
(i) = (a)*3.2808399;
(j) = (a)*0.0328084;
(k) = (a)*2.54;
(l) = (a)*0.3937008;
switch (b)
case 'k' :
{
cout << (c) << " KM" << endl
<< "__________________________________" << endl;
break;
case 'm' :
cout << (d) << " MILES" << endl <<
"________________________________" << endl;
break;
case 'M' :
cout << (e) << " METRE" << endl <<
"__________________________________" << endl;
break;
case 'y' :
cout << (f) << " YARD" << endl <<
"____________________________________" << endl;
break;
case 't' :
cout << (g) << " METRE" << endl <<
"___________________________________" << endl;
break;
case 'T' :
cout << (i) << " FEET" << endl <<
"____________________________________" << endl;
break;
case 'f' :
cout << (h) << " CM" << endl <<
"______________________________________" << endl;
break;
case 'c' :
cout << (j) << " FEET" << endl <<
"______________________________________" << endl;
break;
case 'i' :
cout << (k) << " CM" << endl <<
"_____________________________________" << endl;
break;
case 'n' :
cout << (l) << " INCH" << endl <<
"______________________________________" << endl;
break;
default :
cout << " WRONG KEY PLEASE TRY AGAIN !!!"<< endl;
}
cout << " ENTER 1 TO CONTINUE >>><<< ENTER ANY OTHER # TO GO TO MAIN MENU" << endl
<< "-------------------------" <<endl;
cin >> (u);
}
system("pause");
return main();
{
}
}
}
/* *********************************************************************************************** */
/* *********************************************************************************************** */
int temps()
{
float a;
char b;
float c;
float d;
float e;
float f;
int i = 1;
const float g =1.8;
while (i == 1)
{
cout
<<"##############################################################################" << endl
<<"WELCOME TO THE C.M.D TEMP CONVERTER " << endl
<<"******************************************************************************" << endl
<<"When prompted for unit ********* ********* " << endl
<<" ********* ******* " << endl
<<"Enter f to convert celcius to farenheit **** ** ** " << endl
<<"*************************************** **** ** ** " << endl
<<"Enter c to convert farenheit to celcius **** ** ** *********** " << endl
<<"**************************************** ******** ** ** ********** " << endl
<<"Enter temp ******* ** ** **** " << endl
<<"---------- ** ** ******** " << endl
<<" ** ** ******** ******* "<< endl
<<" ***** **** *** *** " << endl
<<" **** *** *** " << endl
<<" **** *** *** " << endl
<<" **** ***** " << endl;
cin >> (e);
cout << "ENTER UNIT" << endl <<"-----------";
cin >> b;
c = (e)*(g)+32;
d = (e)*.5555557-18;
switch (b)
{
case 'f' :
cout << (c) << " fahrenheit" << endl
<< "----------------------------------------------" << endl;
break;
case 'c' :
cout << d << " celcius" <<
endl << "-----------------------------------------------" << endl;
}
cout <<"TO CONTINUE ENTER 1 >><<< TO go to MAIN MENU ENTER ANY OTHER NUMBER" <<
endl <<"-----------------------------------------------------";
cin >> (i);
}
return main();
}
/* ************************************************************************************************************************************************************************** */
/* ************************************************************************************************************************************************************************** */
int heavy()
{
float a;
float b;
float c;
float d;
float e;
float f;
float g;
float h;
float i;
float j;
float l;
int q =1;
while (q == 1)
{
char k;
cout <<"############################################" << endl
<< "WELCOME TO THE C.M.D WEIGHT CONVERTER >>><<<" << endl
<<"********************" <<
endl <<"|pound to kilo-->k *** **** *** " <<
endl <<"|kilo to pound-->p ************************** " <<
endl <<"|ounce to gram-->g *** ** *** " <<
endl <<"|gram to ounce-->o * ** * " <<
endl <<"|carat to gram-->G * ** * " <<
endl <<"|gram to carat-->t ******* ** ******* " <<
endl <<"|stone to kilo-->K ***** ** ***** " <<
endl <<"kilo to stone--->s ***** ** ***** " <<
endl <<"pound to tone--->t ***** ** ***** " <<
endl <<" tone to pound-->P ** " << endl
<<"******************* ********* " <<
endl <<"ENTER UNITS ************* " <<
endl <<"------------------- *****************"<< endl;
cin >> (k);
cout <<"*************************" << endl << "ENTER THE AMOUNT" << endl <<"-------------"<<endl;
cin >> (a);
(b) =(a)*0.4536;
(c) =(a)*2.2045855;
(d) =(a)*28.35;
(e) =(a)*0.0352734;
(f) =(a)*0.2;
(g) =(a)*5;
(h) =(a)*0.157473;
(i) =(a)*6.3502932;
(j) =(a)*0.0004;
(l) =(a)*2500;
switch (k)
{
case 'k' :
cout << (b) << " KILOS" << endl
<<"--------" << endl;
break;
case 'p' :
cout << (c) << " pounds" << endl
<<"---------" << endl;
break;
case 'g' :
cout << (d) << " grams" << endl
<< "--------" << endl;
break;
case 'o' :
cout <<(e) << " ounces" << endl <<
"---------" << endl;
break;
case 'G':
cout << (f) <<" grams" << endl <<
"---------" << endl;
break;
case 'c' :
cout << (g) <<" carats" << endl <<
"---------" << endl;
break;
case 'K' :
cout << (i) << " kilos" << endl <<
"---------" << endl;
break;
case 's' :
cout << (h) << " stones" << endl <<
"---------" << endl;
break;
case 't' :
cout << (j) << " tones" << endl <<
"--------" << endl;
break;
}
cout << "ENTER 1 TO CONTINUE >><< ENTER ANY OTHER NUMBER TO RETURN TO MAIN MENU" << endl
<<"**********************************************************************";
cin >> (q);
}
return main();
}
endl
main.cpp:143:17: error: ‘system’ was not declared in this scope
Every function need to be declared, "system" is declared in "stdlib.h" just include this file. But it probably not work as excepted, because Windows and Linux has different shells command, and pause may not exists on your system.
Quote:
main.cpp:308:1: error: ‘endl’ does not name a type
And what it doing on the last line of your code?
Also you shouldn't invoke "return main()", because you probably created infinite loop with memory leak. If you want this behavior use "while(1)" or similar construct.
well thx guys for your help, it let go of those errors,
but now like you said
Quote:
Every function need to be declared, "system" is declared in "stdlib.h" just include this file. But it probably not work as excepted, because Windows and Linux has different shells command, and pause may not exists on your system.
i got
:~/cp$ c++ main.cpp -o main
main.cpp: In function ‘int heavy()’:
main.cpp:307:6: error: label ‘BEGIN’ used but not defined
working on the functions .. guess i should just go over it all . any thing else you guys can see ???
well thx guys for your help, it let go of those errors,
but now like you said
i got
:~/cp$ c++ main.cpp -o main
main.cpp: In function ‘int heavy()’:
main.cpp:307:6: error: label ‘BEGIN’ used but not defined
working on the functions .. guess i should just go over it all . any thing else you guys can see ???
Single letter variable names, you can and should use words.
The brace around line 85 should go after the switch, not case.
You have an empty pair of braces at the end of main() for no apparent reason.
EVERY function ends with "return main();", WHY????
EDIT: it occurs to me that maybe you thought "return main();" means return to main(), actually it means, call main() and return its result to the caller. If you just want to return from a function, a plain "return;" will suffice (although it's redundant at the end of a void function).
Single letter variable names, you can and should use words.
The brace around line 85 should go after the switch, not case.
You have an empty pair of braces at the end of main() for no apparent reason.
EVERY function ends with "return main();", WHY????
hehe go easy on me ... does 1,2,4 and5 have to be corrected for it to function ? i would mostly like to just know am compiling the correct way so i can continue to learn .. i had no idea i had broken so many rules dev cpp din't seem to find any errors .
does 1,2,4 and5 have to be corrected for it to function ?
You don't need correct indentation, but it makes it much easier to see other errors (the only way I could see your other problems was to copy the code into Emacs and indent everything).
You don't need meaningful variable names, but it makes it much easier to see what the program is doing.
I think the empty pair of braces is harmless, but it's confusing.
About the "return main();"s, you can compile successfully with them, but your program will get stuck in an infinite loop overflow the stack at run time as mentioned by previous posters. I think you may have misunderstood what it means, see the EDIT in my previous post.
Last edited by ntubski; 10-17-2012 at 11:38 AM.
Reason: return main(); won't really cause an infinite loop.
hehe go easy on me ... does 1,2,4 and5 have to be corrected for it to function ? i would mostly like to just know am compiling the correct way so i can continue to learn .. i had no idea i had broken so many rules dev cpp din't seem to find any errors .
Number 5 is critical; the others are just aesthetics to make your code easier to read/maintain.
ok did some editing this morning .... MORE errors ...
:~/cp$ c++ main.cpp -o main
main.cpp:7:5: warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x [enabled by default]
main.cpp:9:1: error: expected primary-expression before ‘float’
main.cpp:9:1: error: expected ‘}’ before ‘float’
main.cpp:9:1: error: expected ‘,’ or ‘;’ before ‘float’
main.cpp:23:1: error: ‘cout’ in namespace ‘std’ does not name a type
main.cpp:31:1: error: ‘cin’ does not name a type
i have spent a lot of time looking at these lines ,
9, 23 and 31. 7 doesn't seem to be something wrong with the code i just don't know what gnu++ox is ?? and how to enable it .
here is the revision
Code:
#include <cstdlib>
# include <iostream>
using namespace std;
int temps;
int heavy;
int main
{
float a;
char b;
float c;
float d;
float e;
float f;
float g;
float h;
float i;
float j;
float k;
float l;
int u =1;
int n;
std::cout << "ENTER 1 TO USE THE DIST CONVERTER >><< ENTER 2 TO USE THE TEMP CONVERTER"
<<
endl
<<"ENTER 3 TO USE WEIGHT CONVERTER, ENTER ANY OTHER # TO EXIT" <<
endl
<< "CHOSE A CONVERTER"
<<
std::endl;
cin >> (n)
if (n <= 3 && n >=1)
{
if ( n == 3)
heavy();
else if (n==2)
temps();
if (n == 1)
while (u == 1)
{
cout << "---------------------------------------" <<
endl << "WELCOME TO THE C.M.D DIST CONVERTER" <<
endl << "***************************************" <<
endl << "* miles to km.> k" <<
endl << "*km to miles >> m" <<
endl << "*yard to metre> M ********************************************************" <<
endl << "* metre to yard>y ** * * * * * * * * * **" <<
endl << "*foot to metre> t ** * * * * * * * * * **" <<
endl << "*metre to foot> T ********************************************************" <<
endl <<"*foot to cm > f " <<
endl <<"*cm to foot > c" <<
endl <<"* inch to cm > i " <<
endl <<"*cm to inch>c > n" <<
endl << "-----------------"<<
endl <<"ENTER FUNCTION KEY " <<
endl <<"-------------------" <<
endl;
cin >>(b);
cout <<" ENTER THE DISTANCE" << endl
<< "___________________ " << endl;
cin >> (a);
(c) = (a)*1.609;
(d) = (a)/.5936133;
(e) = (a)/1.09;
(f) = (a)*1.09;
(g) = (a)/0.3028;
(h) = (a)*30.48;
(i) = (a)*3.2808399;
(j) = (a)*0.0328084;
(k) = (a)*2.54;
(l) = (a)*0.3937008;
switch (b)
case 'k' :
{
cout << (c) << " KM" << endl
<< "__________________________________" << endl;
break;
case 'm' :
cout << (d) << " MILES" << endl <<
"________________________________" << endl;
break;
case 'M' :
cout << (e) << " METRE" << endl <<
"__________________________________" << endl;
break;
case 'y' :
cout << (f) << " YARD" << endl <<
"____________________________________" << endl;
break;
case 't' :
cout << (g) << " METRE" << endl <<
"___________________________________" << endl;
break;
case 'T' :
cout << (i) << " FEET" << endl <<
"____________________________________" << endl;
break;
case 'f' :
cout << (h) << " CM" << endl <<
"______________________________________" << endl;
break;
case 'c' :
cout << (j) << " FEET" << endl <<
"______________________________________" << endl;
break;
case 'i' :
cout << (k) << " CM" << endl <<
"_____________________________________" << endl;
break;
case 'n' :
cout << (l) << " INCH" << endl <<
"______________________________________" << endl;
break;
default :
cout << " WRONG KEY PLEASE TRY AGAIN !!!"<< endl;
}
cout << " ENTER 1 TO CONTINUE >>><<< ENTER ANY OTHER # TO GO TO MAIN MENU" << endl
<< "-------------------------" <<endl;
cin >> (u);
}
system("pause");
return;
int temps()
{
float a;
char b;
float c;
float d;
float e;
float f;
int i = 1;
const float g =1.8;
while (i == 1)
{
cout
<<"##############################################################################" << endl
<<"WELCOME TO THE C.M.D TEMP CONVERTER " << endl
<<"******************************************************************************" << endl
<<"When prompted for unit ********* ********* " << endl
<<" ********* ******* " << endl
<<"Enter f to convert celcius to farenheit **** ** ** " << endl
<<"*************************************** **** ** ** " << endl
<<"Enter c to convert farenheit to celcius **** ** ** *********** " << endl
<<"**************************************** ******** ** ** ********** " << endl
<<"Enter temp ******* ** ** **** " << endl
<<"---------- ** ** ******** " << endl
<<" ** ** ******** ******* "<< endl
<<" ***** **** *** *** " << endl
<<" **** *** *** " << endl
<<" **** *** *** " << endl
<<" **** ***** " << endl;
cin >> (e);
cout << "ENTER UNIT" << endl <<"-----------";
cin >> b;
c = (e)*(g)+32;
d = (e)*.5555557-18;
switch (b)
{
case 'f' :
cout << (c) << " fahrenheit" << endl
<< "----------------------------------------------" << endl;
break;
case 'c' :
cout << d << " celcius" <<
endl << "-----------------------------------------------" << endl;
}
cout <<"TO CONTINUE ENTER 1 >><<< TO go to MAIN MENU ENTER ANY OTHER NUMBER" <<
endl <<"-----------------------------------------------------";
cin >> (i);
}
return main();
}
/* ************************************************************************************************************************************************************************** */
/* ************************************************************************************************************************************************************************** */
int heavy
{
float a;
float b;
float c;
float d;
float e;
float f;
float g;
float h;
float i;
float j;
float l;
int q =1;
while (q == 1)
{
char k;
cout <<"############################################" << endl
<< "WELCOME TO THE C.M.D WEIGHT CONVERTER >>><<<" << endl
<<"********************" <<
endl <<"|pound to kilo-->k *** **** *** " <<
endl <<"|kilo to pound-->p ************************** " <<
endl <<"|ounce to gram-->g *** ** *** " <<
endl <<"|gram to ounce-->o * ** * " <<
endl <<"|carat to gram-->G * ** * " <<
endl <<"|gram to carat-->t ******* ** ******* " <<
endl <<"|stone to kilo-->K ***** ** ***** " <<
endl <<"kilo to stone--->s ***** ** ***** " <<
endl <<"pound to tone--->t ***** ** ***** " <<
endl <<" tone to pound-->P ** " << endl
<<"******************* ********* " <<
endl <<"ENTER UNITS ************* " <<
endl <<"------------------- *****************"<< endl;
cin >> (k);
cout <<"*************************" << endl << "ENTER THE AMOUNT" << endl <<"-------------"<<endl;
cin >> (a);
(b) =(a)*0.4536;
(c) =(a)*2.2045855;
(d) =(a)*28.35;
(e) =(a)*0.0352734;
(f) =(a)*0.2;
(g) =(a)*5;
(h) =(a)*0.157473;
(i) =(a)*6.3502932;
(j) =(a)*0.0004;
(l) =(a)*2500;
switch (k)
{
case 'k' :
cout << (b) << " KILOS" << endl
<<"--------" << endl;
break;
case 'p' :
cout << (c) << " pounds" << endl
<<"---------" << endl;
break;
case 'g' :
cout << (d) << " grams" << endl
<< "--------" << endl;
break;
case 'o' :
cout <<(e) << " ounces" << endl <<
"---------" << endl;
break;
case 'G':
cout << (f) <<" grams" << endl <<
"---------" << endl;
break;
case 'c' :
cout << (g) <<" carats" << endl <<
"---------" << endl;
break;
case 'K' :
cout << (i) << " kilos" << endl <<
"---------" << endl;
break;
case 's' :
cout << (h) << " stones" << endl <<
"---------" << endl;
break;
case 't' :
cout << (j) << " tones" << endl <<
"--------" << endl;
break;
}
cout << "ENTER 1 TO CONTINUE >><< ENTER ANY OTHER NUMBER TO RETURN TO MAIN MENU" << endl
<<"**********************************************************************";
cin >> (q);
}
return;
}
line 9:1
Code:
int main
{
float a;
char b;
line 23:1
Code:
int n;
std::cout << "ENTER 1 TO USE THE DIST CONVERTER >><< ENTER 2 TO USE THE TEMP CONVERTER"
<<
line 31 is similar to 23 so if i can figure out what am doing wrong there al prolly need to edit all my cout and cin ops ..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.