Hello Guys I am a relatively new coder trying to learn C++ and I am having an issue with my compiler. I do not know why it is giving me the error function definition not allowed here. If anyone could help me it would be greatly appreciated.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main( )
{
//***************************************************************************************//
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//***************************************************************************************/
#include <iostream>
#include <string>
#include <fstream> //you must include this library if you wish to do file i/o
#include <iomanip>
using namespace std;
/*********************************************************
//Following is the declaration of a order record
**********************************************************/
class order_record
{
public:
string pname;
string cname;
double plant_cost;
double quantity;
double purchase_tax;
double net_cost;
double discount;
double total_cost;
};
//Prototypes for your functions: input, output, and process will go here
void input(ifstream&, order_record&);
void output(const order_record&);
void process(order_record&);
//Function Implementations will go here
///*************************************************************************************
//Name: input
//Precondition: State what is true before the function is called.
// Example: the varialbes (formal parameters) have not been initialized
//Postcondition: State what is true after the function has executed.
// Example: the varaibles (formal parameters) have been initialized
//Description:
// Example:Get input (values of plant name, county name, plant cost, quantity )
//from data file order record (p_record).
//PURPOSE: SHOW ME THAT YOU KNOW HOW TO READ INPUT AND USE INPUT (CALL BY VALUE) & OUTPUT (CALL BY VALUE) PARAMETERS
//*************************************************************************************
void input(ifstream& in, order_record& plant_record) // example using the call by reference mechanism in C++ -- call record is passed by reference --note & operator
{
in >> plant_record.pname;
in >> plant_record.cname;
in >> plant_record.plant_cost;
in >> plant_record.quantity;
//add more code to read the rest of the fields (county name, plant cost, quantity) into the
//order record, plant_record.
}
///*************************************************************************************
//Name: output
//Precondition: State what is true before the function is called.
//Postcondition: State what is true after the function has executed.
//Description: Describe what the function does (purpose).
//*************************************************************************************
void output(const order_record& plant_record)
{
//Use thee following statement to help you format you our output. These statements are called the magic formula.
cout.setf(ios::showpoint);
cout.precision(2);
cout.setf(ios::fixed);
/********************************************/
cout << plant_record.pname << "\t";
cout << plant_record.cname << "\t"
cout << plant_record.plant_cost << "\t"
cout << plant_record.quantity << "\t"
cout << plant_record.net_cost << "\t"
cout << plant_record.purchase_tax << "\t"
cout << plant_record.discount << "\t"
cout << plant_record.total_cost<< "\t"
}
///*************************************************************************************
//Name: process
//Precondition: The state what is true before the function is called.
//Postcondition: State what is true after the function has executed.
//Description: Describe what the function does (purpose).
//*************************************************************************************
//Note: there is one 1 input/output parameter
void process(order_record & plant_record)
{
plant_record.net_cost = plant_record.quantity * plant_record.plant_cost;
plant_record.total_cost = plant_record.net_cost + plant_record.purchase_tax - plant_record.discount;
if( plant_record.quantity <=0 )
plant_record.discount = 0;
else if(plant_record.quantity >= 1 && plant_record.quantity <=5 )
plant_record.discount = .01 * plant_record.net_cost;
else if(plant_record.quantity >= 6 && plant_record.quantity <=11 )
plant_record.discount = .03 * plant_record.net_cost;
else if(plant_record.quantity >= 12 && plant_record.quantity <=20)
plant_record.discount = .05 * plant_record.net_cost;
else if(plant_record.quantity >= 21 && plant_record.quantity <=50)
plant_record.discount = .08 * plant_record.net_cost;
else if(plant_record.quantity > 50)
plant_record.discount = .12 * plant_record.net_cost;
if(plant_record.cname == "dade" || plant_record.cname == "Dade")
plant_record.purchase_tax = .065 * plant_record.net_cost;
else if(plant_record.cname == "broward" || plant_record.cname == "Broward")
plant_record.purchase_tax = .06 * plant_record.net_cost;
else if(plant_record.cname == "palm" || plant_record.cname == "Palm")
plant_record.purchase_tax = .07 * plant_record.net_cost;
else
cout <<"Please enter the correct infromation";
//put your code here to process/calculate the net cost, purchase tax, discount, and total cost
//for the plant_record
}
//Here is your driver to test the program
int main()
{
order_record purchase_record;
string pname, cname;
double cost;
int quantity;
ifstream in; //declaring an input file stream
in.open("nursery_stock.txt"); //opening the input file stream
if (in.fail())
{
cout << "Input file did not open correctly" << endl;
}
else
{ //the following while loop processes one order record at a time
while (!in.eof())
{
input(in, purchase_record);
process(purchase_record);
output(purchase_record);
}
}
in.close(); //always close opened files.
return 0;
}
I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.
Please use the “preformatted text” tool in the editor (</>
) to add backticks around text.
See this post to find the backtick on your keyboard.
Note: Backticks are not single quotes.
Running in repl.it, I got the following errors
errors
 clang++-7 -pthread -std=c++17 -o main main.cpp
In file included from main.cpp:15:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/fstream:42:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0/bits/basic_file.h:40:
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0/bits/c++io.h:39:1: error:
namespaces can only be defined in global or namespace scope
{
^
In file included from main.cpp:15:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/fstream:42:
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/x86_64-linux-gnu/c++/7.5.0/bits/basic_file.h:45:1: error:
namespaces can only be defined in global or namespace scope
{
^
In file included from main.cpp:15:
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/fstream:48:1: error:
namespaces can only be defined in global or namespace scope
{
^
In file included from main.cpp:15:
In file included from /usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/fstream:1081:
/usr/bin/../lib/gcc/x86_64-linux-gnu/7.5.0/../../../../include/c++/7.5.0/bits/fstream.tcc:43:1: error:
namespaces can only be defined in global or namespace scope
{
^
main.cpp:59:6: error: function definition is not allowed here
{
^
main.cpp:76:6: error: function definition is not allowed here
{
^
main.cpp:105:6: error: function definition is not allowed here
{
^
main.cpp:139:6: error: function definition is not allowed here
{
^
main.cpp:166:7: error: expected '}'
}
^
main.cpp:7:2: note: to match this '{'
{
^
9 errors generated.
compiler exit status 1
This is telling me that you have some namespaces and function definitions in the wrong places, so let’s look.
int main()
{
...
// Some function definitions.... uh oh....
}
In C/C++, every function should be separate. You can’t define functions inside of functions. (You sort of can if you do it right and are super careful, but it’s not a good idea, so let’s just say “you can’t” and leave it at that!).
The first thing you will want to do is to remove this outer
int main ()
{
}
Once you have done that, you will get some errors because
//***************************************************************************************//
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//***************************************************************************************//
needs to be moved inside of main and you are missing some semicolons.
1 Like
ok I will work on it thank you so much for the help
I am still seeing error and I also forgot to mention that this program is meat to run a file which is given to us.
What is your updated code?
Its the same thing
as what you see above the formula in between the comments are not comments themselves and are within the main function.
I am working on a skeletal project which my professor gave to us he said that it should have been a working program from the start so I don’t think changing the location of the main function would fix the problem.
If it’s the same and you didn’t make any changes, then it’s still broken.
You’ve inserted what looks like the provided code inside of a main
which is breaking everything.
1 Like
yeah I I guess I have to talk to my professor tomorrow and see what he says
But thank you for the help again I appreciate it.
… When I delete the first 12 lines of your code, it compiles with almost no errors for me as long as I add semicolons in the output
cout
s. If you try that, it’ll work…
I really think you pasted the given code in with a few more lines than you were supposed to.
I will try this and let you know
When you say first 12 lines of code you are talking about the ones in the main function starting from
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
I removed
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
int main( )
{
//***************************************************************************************//
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//***************************************************************************************/
I did what you said and the code compiled without any errors but I am going to try to run in the file and see what happens
It said the input file did not open correctly.
Is the input file in the same directory and named as expected?
1 Like
It is in the same directory and same name.
Hi guys, I am kind of new to programming in C++ and I am currently working on a program that runs a an input file given to us by the professor. I set up an if-else statement that is supposed to display an error message if the program does not run smoothly and for the life of me I cannot figure out why the file is not running. Than you for taking the time to read this haha and if anyone is able to help it would be greatly appreciated
Here is the input file labeled nursery_stock.
owl |
dade |
10.55 |
100 |
hibiscus |
broward |
15.82 |
15 |
rose |
dade |
9.99 |
45 |
carnation |
palm |
7.99 |
32 |
rose |
palm |
7.99 |
60 |
widow |
palm |
25.75 |
5 |
carnation |
dade |
12.55 |
10 |
carnation |
dade |
12.55 |
8 |
lilly |
broward |
6.92 |
150 |
xerabtgemum |
palm |
13.63 |
50 |
yarrow |
dade |
22.85 |
20 |
zenobia |
palm |
37.19 |
32 |
zephyranthes |
broward |
62.82 |
40 |
daisy |
broward |
15.99 |
80 |
aconitum |
dade |
30.02 |
72 |
amaryllis |
dade |
16.14 |
65 |
bogonia |
broward |
18.45 |
3 |
bellflow |
broward |
2.96 |
200 |
bergenia |
palm |
85.92 |
10 |
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
class order_record
{
public:
string pname;
string cname;
double plant_cost;
double quantity;
double purchase_tax;
double net_cost;
double discount;
double total_cost;
};
void input(ifstream&, order_record&);
void output(const order_record&);
void process(order_record&);
/
void input(ifstream& in, order_record& plant_record) // example using the call by reference mechanism in C++ -- call record is passed by reference --note & operator
{
in >> plant_record.pname;
in >> plant_record.cname;
in >> plant_record.plant_cost;
in >> plant_record.quantity;
//add more code to read the rest of the fields (county name, plant cost, quantity) into the
//order record, plant_record.
}
void output(const order_record& plant_record)
{
//Use thee following statement to help you format you our output. These statements are called the magic formula.
cout.setf(ios::showpoint);
cout.precision(2);
cout.setf(ios::fixed);
cout << plant_record.pname << "\t";
cout << plant_record.cname << "\t";
cout << plant_record.plant_cost << "\t";
cout << plant_record.quantity << "\t";
cout << plant_record.net_cost << "\t";
cout << plant_record.purchase_tax << "\t";
cout << plant_record.discount << "\t";
cout << plant_record.total_cost<< "\t";
}
void process(order_record & plant_record)
{
plant_record.net_cost = plant_record.quantity * plant_record.plant_cost;
plant_record.total_cost = plant_record.net_cost + plant_record.purchase_tax - plant_record.discount;
if( plant_record.quantity <=0 )
plant_record.discount = 0;
else if(plant_record.quantity >= 1 && plant_record.quantity <=5 )
plant_record.discount = .01 * plant_record.net_cost;
else if(plant_record.quantity >= 6 && plant_record.quantity <=11 )
plant_record.discount = .03 * plant_record.net_cost;
else if(plant_record.quantity >= 12 && plant_record.quantity <=20)
plant_record.discount = .05 * plant_record.net_cost;
else if(plant_record.quantity >= 21 && plant_record.quantity <=50)
plant_record.discount = .08 * plant_record.net_cost;
else
plant_record.discount = .12 * plant_record.net_cost;
if(plant_record.cname == "dade" || plant_record.cname == "Dade")
plant_record.purchase_tax = .065 * plant_record.net_cost;
else if(plant_record.cname == "broward" || plant_record.cname == "Broward")
plant_record.purchase_tax = .06 * plant_record.net_cost;
else if(plant_record.cname == "palm" || plant_record.cname == "Palm")
plant_record.purchase_tax = .07 * plant_record.net_cost;
else
cout <<"Please enter the correct infromation";
}
int main()
{
order_record purchase_record;
string pname, cname;
double cost;
int quantity;
ifstream in; //declaring an input file stream
in.open("nursery_stock.txt"); //opening the input file stream
if (in.fail())
{
cout << "Input file did not open correctly" << endl;
}
else
{ //the following while loop processes one order record at a time
while (!in.eof())
{
input(in, purchase_record);
process(purchase_record);
output(purchase_record);
}
}
in.close(); //always close opened files.
return 0;
}
Please do not create duplicate topics for the same question. I have merged your threads.