Click here to Skip to main content
15,891,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <iostream>
#include <fstream>
#include <cmath>


using namespace std;

int main()
{
//Declaration
ifstream fp1;
ofstream fp2;
char inputFile[20],outputFile[20],hn1[20],hn2[20],hn3[20],Awidth[20],Vwidth[20],mean[20],ans,text1[30],text2[30],text3[30];
double h1[10],h2[10],h3[10],m[10],Aw[10],Vw[10],Area1[10],Area2[10],Area3[10], Volume1[10], Volume2[10];
int i,n;

//Input file name
again:
cout << "Input file name: ";
cin >> inputFile;

//Input file protocol

fp1.open(inputFile);
if(!fp1)
{
	cout << "Input File " << inputFile << " cannot be opened.";
	cout << "\nDo You Like to Try Again? (Y/N)";
	cin >> ans;
	if(ans == 'y' || 'Y') goto again;
	return 0;
}
cout << "\nInput File " << inputFile << " read successfully.";

//Program read Input file

fp1 >> text1;
fp1 >> text2;
fp1 >> text3;
fp1 >> hn1 >> hn2 >> hn3 >> Awidth >> Vwidth >> mean;
for(i = 1; i<=1000; i++)	
	{
	fp1 >> h1[i] >> h2[i] >> h3[i] >> Aw[i] >> Vw[i] >> m[i];
								
	}

//Processing Data
Area1[i] = 0.0;
Area2[i] = 0.0;
Area3[i] = 0.0;

 
Area1[i] = h1[i] * (Aw[i] + (m[i] * h1[i]));
Area2[i] = h2[i] * (Aw[i] + (m[i] * h2[i]));
Area3[i] = h3[i] * (Aw[i] + (m[i] * h3[i]));

Volume1[i] = 0.0;
Volume2[i] = 0.0;

Volume1[i] = Vw[i] * (Area1[i] + (2 * Area2[i]) + Area3[i]) / 2; //End area method
Volume2[i] = Vw[i] * (Area1[i] + (4 * Area2[i]) + Area3[i]) / 3; //Prismoidal Rule

//Enter Output filename
cout << "\n\nSave Data to Output File? (Y/N) ";
	cin >> ans;
	if (ans == 'y'|| ans == 'Y') 
	    goto next;
	else
	    goto next2;

next:
	cout << "\nEnter Output Filename: ";
	cin >> outputFile;
	
//Output file protocol
	fp2.open(outputFile);
	if(!fp2)
		{
			cout << "Output File " << outputFile << " cannot be opened.";
			cout << "\nDo You want to Try Again? (Y/N)";
			cin >> ans;
			if(ans == 'y' || 'Y') goto again;
			return 0;
		}
	cout << "\nOutput File " << outputFile << " readed successfully.";

//Print output
fp2 << "\n" << text1;
fp2 << "\n" << text2;
fp2 << "\n" << text3;
fp2 << "\n" << hn1 << "\t" << hn2 << "\t" << hn3 << "\t" << Awidth << "\t" << Vwidth << "\t" << mean; 
for (i = 1;i < 2; i++) 
		{
		fp2 << "\n" << h1[i] << "\t" << h2[i] << "\t" << h3[i] << "\t" << Aw[i] << "\t" << Vw[i] << "\t" << m[i];
		}

fp2 << "\nArea 1= " << Area1[i];
fp2 << "\nArea 2= " << Area2[i];
fp2 << "\nArea 3= " << Area3[i];

fp2 << "\nVolume by End area= " << Volume1[i];
fp2 << "\nVolume by Prismoidal Rule= " << Volume2[i];

//Output read file
next2:
cout << "\n" << text1;
cout << "\n" << text2;
cout << "\n" << text3;
cout << "\n" << hn1 << "\t" << hn2 << "\t" << hn3 << "\t" << Awidth << "\t" << Vwidth << "\t" << mean;
for (i = 1;i < 2; i++)
		{
		cout << "\n" << h1[i] << "\t" << h2[i] << "\t" << h3[i] << "\t" << Aw[i] << "\t" << Vw[i] << "\t" << m[i];
		}

cout << "\nArea 1= " << Area1[i];
cout << "\nArea 2= " << Area2[i];
cout << "\nArea 3= " << Area3[i];

cout << "\nVolume by End area= " << Volume1[i];
cout << "\nVolume by Prismoidal Rule= " << Volume2[i];


fp1.close();
fp2.close();
return 0;


//data.txt (below)
/*****
Formation_width=12.00m
Formation_Height=3.00m,4.50m,6.00m
Intervals=30.00m.
Height1	Height2	Height3	Area(w)	VolumeW	meanM
3.00	4.50	6.00	12.00	30.00	2
****/

}


What I have tried:

-I tried to run the data but the cout is not showing and it only shows "read successfully" message.
-My best guess is the calculation (//Processing Data) section somehow broke my program because when I delete that said section, my program can run normally until the end. I'm not sure if the arrangement is the problem or the calculation is the problem.

-My calculation involving finding the Area & Volume(for reference if needed):
-Area (1) = h1 (w + mh1)
= 3.00 [ 12.00 + (2 x 3.00) ] = 54.00 m2
-Volume = w ( A1 + 2A2 + A3 ) / 2
= 30.00 (54.00 + 189.00 + 144.00) / 2
= 5805.0 m3
Posted
Updated 10-Jan-21 7:12am
v2

Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!

[edit]
I have added pre tags to preserve your formatting, but all that really did was make your code less likely for people to read, particularly when you see bits like this:
C++
cout << "\n\nSave Data to Output File? (Y/N) ";
	cin >> ans;
	if (ans == 'y'|| ans == 'Y') 
	    goto next;
	else
	    goto next2;

next:
	cout << "\nEnter Output Filename: ";
Sort your indentation out, get rid of every label and forget that goto even exists for several years, and you'll understand when it is appropriate to use and when it isn't. And I haven't used a goto in any code for nearly 2 decades ... so a relatively trivial task like this should not need any at all. Using them implies that you didn't sit down and think about your design before you sat down and rushed into code - and that's probably why it's so ugly, difficult to follow, and doesn't actually work! five minutes of planning can save you hours of pain and debugging, honest.
[/edit]
 
Share this answer
 
v2
Comments
CrimsonRed 10-Jan-21 1:51am    
Thanks for the advice! I somehow manage to solve the problem.
Stefan_Lang 10-Jan-21 17:38pm    
I wholeheartedly agree, especially to your edit. I haven't used goto in the past ~35 years, and although at times I was (grudgingly) talked into to leaving an occasional goto be (rather than eliminating it), I will no longer stand it. People should go look for articles and books on Clean Code rather than hack their way through an algorithm without thinking or planning.

(and have a 5 - sadly I can't give 6)
Stefan_Lang 11-Jan-21 5:30am    
If that is the kind of code you get taught, then you really need a different teacher. Or just a teacher.

For starters, google on "Clean Code" principles. Here's a nice article that explains what "Clean Code" means and shows with a few examples how to write Clean Code.
https://simpleprogrammer.com/clean-code-principles-better-programmer/

Although the examples are not for C++, the principles are just the same. You can find a lot more articles and examples in the internet, and there are good books, video courses and other materials to purchase if you are really interested.

Note that the term 'Clean Code' is sometimes interpreted differently. E. g. some claim that in order to understand your code well, you need good comments in your code. I used to follow that standard as well, but over the years I've learned the hard way that the best code is code that someone else can read and understand without comments and documentation of any kind! That doesn't mean that comments are generally bad, or good. It means that in the end the most meaningful measurement of clean code is that others can read your code well.

And when I say 'others', that does include your future self in a couple of weeks or months: personally I've found that the person having to read my own code is me, most of the time - and I'm always happy when I find out that I did write my code well enough that I still understand it after months or even years!
What about using the debugger? Read this Debugging chapter and other parts of this tutorial to kickstart learning to debug.

Tip: it also a good idea to write some test code to make that process easier and maintain code. I do it regularly :-O
 
Share this answer
 
Comments
CrimsonRed 11-Jan-21 1:29am    
Thanks for the advice! I'm using Bloodshed DevC++, so i'm not sure whether debugging is a feature. I only applied what have been taught to me which is around 36 hours of programming class.
The major error in your code is trying to access items after the bounds of (several) arrays:
Quote:
for(i = 1; i<=1000; i++)
{
fp1 >> h1[i] >> h2[i] >> h3[i] >> Aw[i] >> Vw[i] >> m[i];

}

//Processing Data
Area1[i] = 0.0;
Area2[i] = 0.0;
Area3[i] = 0.0;


Area1[i] = h1[i] * (Aw[i] + (m[i] * h1[i]));
Area2[i] = h2[i] * (Aw[i] + (m[i] * h2[i]));
Area3[i] = h3[i] * (Aw[i] + (m[i] * h3[i]));

Volume1[i] = 0.0;
Volume2[i] = 0.0;

Volume1[i] = Vw[i] * (Area1[i] + (2 * Area2[i]) + Area3[i]) / 2; //End area method
Volume2[i] = Vw[i] * (Area1[i] + (4 * Area2[i]) + Area3[i]) / 3; //Prismoidal Rule

All the above accesses are wrong.

That said, you are using C++ like C (the Gods of C++ will punish you for this).
C++ provides collections (e.g. vector) and string.
Note that even plain C provides struct to logically group your variables.
 
Share this answer
 
Comments
CrimsonRed 11-Jan-21 1:24am    
Thanks for the help! Like you said, I somehow manage to solve that problem.
Here's one way you can write the code to avoid using the first goto.
C++
//Input file name
    bool again = true;
    while( again )
    {
        cout << "Input file name: ";
        cin >> inputFile;

        //Input file protocol

        fp1.open(inputFile);
        if( fp1.is_open() )
        {
            break;
        }
        else
        {
            cout << "Input File " << inputFile << " cannot be opened.";
            cout << "\nDo You Like to Try Again? (Y/N)";
            cin >> ans;
            again = ( tolower( ans ) == 'y' );
            if( ! again )
                return 0;
        }
    }
 
Share this answer
 
Comments
CrimsonRed 11-Jan-21 1:34am    
Thanks for the advice! I'll use this as a reference.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900