• In the middle of programming a C++ application for class... Need quick help.
    22 replies, posted
-snip-
The main function doesn't know about the other functions because they are later to be compiled. Either put main at the end of the file or use function prototypes.
-snip-
Or before main() you could have put [cpp]int menu(); void program1(); void program2();[/cpp] So then main() would know about them even they're after it.
Why so many comments? Also, space out your operators.
-snip-
It means putting a space around the operators. (<<, >>, <, >, +, -, =...)
-snip-
[QUOTE=newbs;25097270]Meh, waste of time IMO. Anyways, I cleaned up my code and turned it in. So far the class has been extremely easy.[/QUOTE] Wow, you made me snort/lol. [editline]06:13PM[/editline] That is honestly the stupidest thing I've ever heard. Really, if hitting the spacebar to make code readable is a "waste of time"...
is this a highschool class? I have to wait for sophomore to get a CS class :(
[QUOTE=toaster468;25097401]is this a highschool class? I have to wait for sophomore to get a CS class :([/QUOTE] Most high schools I know are like that. Grade 9 - Introduction to computers Grade 10 - An easy language Grade 11 - OOP Grade 12 - More OOP
[QUOTE=toaster468;25097401]is this a highschool class? I have to wait for sophomore to get a CS class :([/QUOTE] I'm fairly certain that it's a college level class.
[QUOTE=newbs;25095890]Because everyone in the class is a dumbass, and I'll get super jewed on grading if I don't literally comment on everything.[/QUOTE] Comments that explain useful things about how the program works, or why it does what it does, are fine. But: [QUOTE=newbs;25095740][code]ifstream myinput;//Creates a new ifstream object called myinput[/code][/QUOTE] Commenting the obvious is pointless, and makes [i]you[/i] look like a dumbass to anyone reading your code. Regarding spacing: [QUOTE=newbs;25097270]Meh, waste of time IMO.[/QUOTE] Not as much as you might think. A little whitespace goes a long way for readability; it can make your program much less "OMG wall of code" when you come back to it later.
[QUOTE=Wyzard;25099885]Not as much as you might think. A little whitespace goes a long way for readability; it can make your program much less "OMG wall of code" when you come back to it later.[/QUOTE] At the same time, you don't want to use whitespace just because you can. Use it to divide your code into logical sections.
-snip-
For comparison, here's the same code with some formatting tweaks and more descriptive comments. Nothing is changed but whitespace and comments, and two string literals in cout statements split across multiple lines in the source code [code] #include <iostream> #include <fstream> using namespace std; void main(){ // Ask the user which program to run int temp = menu(); // Carry out the user's choice if (temp = 1) { program1(); } else { program2(); } } // Asks the user which program they'd like to run and returns their selection. // 1: Write // 2: Read int menu(){ // Display the choices cout << "Which program would you like to run?\n" << "1) Write\n" << "2) Read" << endl; // Read the user's answer int choice = -1; while (choice < 0 || choice > 1) { // Loops until the user enters a valid choice cout << "Please enter a choice: " << endl; cin >> choice; } return choice; } // Reads numbers typed by the user and writes them to a file void program1() { // Open the file ofstream myoutput; myoutput.open("test.dat"); float temp; // Will hold the user's input // Ask the user for five numbers and write each to the file int count = 1; while (count <= 5){ // Prompt the user for the number cout << "Enter number " << count << " :"; cin >> temp; // Write it to the file myoutput << temp << endl; // Increment the counter for next time through the loop count++; } // Done with output, so close the file myoutput.close(); } // Reads and displays numbers from a file, as well as their sum void program2() { // Open the file ifstream myinput; myinput.open("test.dat"); // Read and sum five numbers float num1, num2, num3, num4, num5, numSum; myinput >> num1; myinput >> num2; myinput >> num3; myinput >> num4; myinput >> num5; numSum = num1 + num2 + num3 + num4 + num5; // Display the numbers and the sum cout << "\n\t" << num1 << "\n\t" << num2 << "\n\t" << num3 << "\n\t" << num4 << "\n\t" << num5 << "\n+____" << "\n\t" <<numSum; } [/code] [b]Edit:[/b] BTW, your "(temp = 1)" in main() should be "(temp == 1)".
[QUOTE=Klownox;25097485]Grade 9 - Introduction to computers Grade 10 - An easy language Grade 11 - OOP Grade 12 - More OOP[/QUOTE] You're lucky. For my school, it's: Year 9 - VB6 Year 10 - SQL in MS Access Year 11 - Nothing (although you can accelerate to Year 12's VB6) Year 12 - VB6
When I was in high school, we had nothing that even remotely resembled programming.
Year 9 - Nothing Year 10 - Nothing Year 11 - Nothing Year 12 - Nothing yeaaaaaaa
[QUOTE=newbs;25101778]Personally I think using space between expressions are a waste of time. As long as I divide my processes up into logical units I should be fine.[/QUOTE] That's your preference. However, most people use spaces and I guarantee you'll have to adapt to them rather than the other way around. :)
Oh wow... that code burns my eyes Please please PLEASE use whitespace to space out your code a bit. To anyone else, your code is very hard to read because you just haven't bothered with whitespace where you don't technically NEED it
If you plan on doing anything with programming later in life you'll want to get good at using whitespace and formatting so you can easily share code with your team. Nobody is going to like your code if they have to take a few minutes to look at to see what it does, they should know just by the comments and the layout.
[cpp]//this content is part of a textfile containing C++-code, intended to be compiled by a C++ compiler and linked into an executable //this is a comment, describing the purpose and usage of this file //this is a comment, describing the comment which describes the purpose and usage of this file //I can see forever[/cpp]
Sorry, you need to Log In to post a reply to this thread.