• 0

[C++] Fixing few bugs on a few programs


Question

Still learning C++, thought I knew more than I did and was quite wrong, so reaching out for a little help. Below are three sources for a few basic programs I'm writing, a little help with each would be great! The funciton/error of the programs are below.

 

Program 1:

//Counter program, count to, count from - count to work fine
//but the count by function doesn't, as in if it's above 1
//it doesn't work, like count 1 - 10 by two should be
// 2 4 6 8 10, but it outputs something else and 
//not sure how to fix this

#include <iostream>
#include <iomanip>
using namespace std;

int main(){
	int c1, c2, c3, c4;

	cout << "Please enter where you would like to count to: ";
	cin >> c1;
	for (int count = 1; count <= c1; count++){
		cout << count << "    ";
	}
	cout << endl;

	cout << "Please enter where you would like to count from: ";
	cin >> c2;
	cout << "Please enter where you would like to count to: ";
	cin >> c3;
	cout << "Please enter what you would like to count by: ";
	cin >> c4;

	for (int count2 = c2; count2 <= c3; count2++){
		//int count3 = count2 * c4;
		cout << count2 << "    ";
	}
	cout << endl;

	system("PAUSE");
	return 0;
}

Program 2

//for some reason the math isn't right in this, and the loop makes the 
//sum wrong
#include <iostream>
#include <iomanip>
using namespace std;

int main(){
	int n_grades = 0, grade = 0;
	double sum_grades = 0, average_grades = 0;
	cout << setprecision(2) << setiosflags(ios::fixed) << setiosflags(ios::showpoint);

	cout << "How many grades are you going to enter?: \n";
	cin >> n_grades;

	for (int count = 1; count <= n_grades; count++){
		cout << "Please enter grade: \n";
		cin >> grade;
		sum_grades = grade + grade;
	}

	average_grades = sum_grades / n_grades;

	cout << "The total number of grades are: " << n_grades << endl;
	cout << "The sum of all grades are: " << sum_grades << endl;
	cout << "The average of all the grades are: " << average_grades << endl;

	system("PAUSE");
	return 0;
}

Program 3

//Need to figure out compound/simple interest, no ######ing idea what I'm doing lol


#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main(){
	double prin, rate, time, simpleInterest, simpleBalance, compoundInterest, compoundBalance, final_rate;
	cout << setprecision(2) << setiosflags(ios::fixed) << setiosflags(ios::showpoint);

	cout << "Enter principle: \n";
	cin >> prin;
	cout << "Enter rate(%): \n";
	cin >> rate;
	cout << "Enter Time(years): \n";
	cin >> time;


	final_rate = rate / 100;
	simpleInterest = prin*final_rate*time;
	compoundInterest = prin*pow(1 + rate / 100, time);
	simpleBalance = prin + simpleInterest;
	compoundBalance = prin + compoundInterest;

	//output - simple interest
	cout << "Simple Interest \n";
	cout << "Interest came to: " << simpleInterest << endl;
	cout << "Balance is: " << simpleBalance << endl;

	//output - compound interest
	cout << "Compound Interest \n";
	cout << "Interest came to: " << compoundInterest << endl;
	cout << "Balance came to: " << compoundBalance << endl;

	system("PAUSE");
	return 0;
}

Thank you!

Link to comment
Share on other sites

10 answers to this question

Recommended Posts

  • 0

for (int count2 = c2; count2 <= c3; count2+abs(c4)){

        cout << count2 << " ";

    }

Loops infinitely when compiled  

Link to comment
Share on other sites

  • 0

Yeah sorry about that

 

Should probably be count2 += abs(c4)

Thank you very much! what is the abs function however?

 

In Program 2 inside the for loop you have:

 

sum_grades = grade + grade;

How else can I monitor each grade that gets added up by the user input though?

Link to comment
Share on other sites

  • 0

Thank you very much! what is the abs function however?

 

How else can I monitor each grade that gets added up by the user input though?

 

abs(x) is absolute value of 'x'. written as '|x|' in math.

for the sum of the grades you are adding it twice so it looks like sum_grades equals 'grade' times 2 and you are assigning it not incrementing it so 'sum_grades' will always be twice the value of the last user input for grade.

Link to comment
Share on other sites

  • 0

abs(x) is absolute value of 'x'. written as '|x|' in math.

 

 

You could replace the abs function with some validation of the input.

 

Without validation if a user enter -2 as the incremental step (c4) then you'll enter an infinite loop cause count2 will never be bigger than c3 (unless it is at the beginning in which case the loop wont be run).

Link to comment
Share on other sites

  • 0

abs(x) is absolute value of 'x'. written as '|x|' in math.

for the sum of the grades you are adding it twice so it looks like sum_grades equals 'grade' times 2 and you are assigning it not incrementing it so 'sum_grades' will always be twice the value of the last user input for grade.

So should I remove sum grades from the for loop and do sum_grades += grade ?

Link to comment
Share on other sites

  • 0

Do you know that you can debug your program by pressing F10 in Visual Studio, which will start the program, attach the debugger, and break execution on the first line? You can then continue execution with F10, line-by-line, observing what your program is doing and looking at the value of the different variables simply by putting your mouse cursor on them (or looking at the "Autos" or "Locals" window).

 

You don't seem to have a problem with C++ syntax, you just seem to have difficulty reasoning about what your code does. Debug it and see for yourself; that'll be immensely more efficient and instructive than asking random people on forums. The earlier you learn that skill the faster you'll learn programming.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.