Post: C++ Help
01-27-2011, 01:45 AM #1
fightinillini94
Climbing up the ladder
(adsbygoogle = window.adsbygoogle || []).push({}); Hey guys I just learned the basics of C++ yesterday and decided to make a simple program that solves infinite geometric sequences. I picked this because we just learned it in trig/pre-calc. Here is my code so far:

    
#include <iostream>
#include <cmath>
#include <cstdlib>


int main(){
using namespace std;

double a;
double b;
double c;
double d;
double e;
double f;
double g;
double y;
double n;

cout << "Welcome to Justin's Infinite Geometric Solver!" << endl;
g=y;
while (g=y){
cout << "Input the first term of the infinite geometric series: " << endl;
cin >> a;
cout << "Input the second term of the infinite geometric series: " << endl;
cin >> b;
cout << "Input the third term of the infinite geometric series: " << endl;
cin >> c;
d = abs(c/b);
e = 1 - d;
if(d > 1){
cout << "Diverges!" << endl;
cout << "Would you like to find another sequence? (y or n)" << endl;
cin >> g;
}
else{
f = a/e;
cout << "The sum of this infinite geometric sequence is " << f << endl;
cout << "Would you like to find another sequence? (y or n)" << endl;
cin >> g;
}
}

cin.get();
return 0;
}


The problem that I have run into is the while function. When the program asks if you want to find another sum, it infinitely loops. Can anybody help me solve this problem?
01-27-2011, 02:13 AM #2
El Violador
< ^ > < ^ >
Originally posted by fightinillini94 View Post
Hey guys I just learned the basics of C++ yesterday and decided to make a simple program that solves infinite geometric sequences. I picked this because we just learned it in trig/pre-calc. Here is my code so far:

    
#include <iostream>
#include <cmath>
#include <cstdlib>


int main(){
using namespace std;

double a;
double b;
double c;
double d;
double e;
double f;
double g;
double y;
double n;

cout << "Welcome to Justin's Infinite Geometric Solver!" << endl;
g=y;
while (g=y){
cout << "Input the first term of the infinite geometric series: " << endl;
cin >> a;
cout << "Input the second term of the infinite geometric series: " << endl;
cin >> b;
cout << "Input the third term of the infinite geometric series: " << endl;
cin >> c;
d = abs(c/b);
e = 1 - d;
if(d > 1){
cout << "Diverges!" << endl;
cout << "Would you like to find another sequence? (y or n)" << endl;
cin >> g;
}
else{
f = a/e;
cout << "The sum of this infinite geometric sequence is " << f << endl;
cout << "Would you like to find another sequence? (y or n)" << endl;
cin >> g;
}
}

cin.get();
return 0;
}


The problem that I have run into is the while function. When the program asks if you want to find another sum, it infinitely loops. Can anybody help me solve this problem?


im very new to c++ too so I may not be correct about this:
what i think is where you have "while(g=y){}"
i think it loops because neither "g" nor "y" changes value in your function, therefore g is equal to y and always will be.
also, it could be because (g=y) rather than the boolean value (g==y)
im very new to this so once again, dont take this as definite but its possible.

The following user thanked El Violador for this useful post:

fightinillini94
01-27-2011, 04:18 AM #3
fightinillini94
Climbing up the ladder
Originally posted by Ar88rA View Post
im very new to c++ too so I may not be correct about this:
what i think is where you have "while(g=y){}"
i think it loops because neither "g" nor "y" changes value in your function, therefore g is equal to y and always will be.
also, it could be because (g=y) rather than the boolean value (g==y)
im very new to this so once again, dont take this as definite but its possible.


Thanks for the reply. I tried (g==y) and it still looped. I just need to figure out how to get the program to stop and check with the user before it loops again
01-28-2011, 05:37 AM #4
3arc
Meow.
I'll fix this up for you, You really shouldn't use single-letter variable names, it's (really) hard to read.
01-28-2011, 05:46 AM #5
this is C#.... C++ is like

If checkbox1.checked = true then
webbrowser1.refresh()
end if
01-28-2011, 05:52 AM #6
3arc
Meow.
Originally posted by Marvin1554
this is C#.... C++ is like

If checkbox1.checked = true then
webbrowser1.refresh()
end if


No, what OP is doing is C++.

@OP: I fixed up your code, but for ease of use I added a second function to handle exiting the program, I also had to prototype that function.

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

int exit();

int main() {
double a;
double b;
double c;
double d;
double e;
double f;

cout << "Welcome to Justin's Infinite Geometric Solver!" << endl;
cout << "Input the first term of the infinite geometric series: " << endl;
cin >> a;
cout << "Input the second term of the infinite geometric series: " << endl;
cin >> b;
cout << "Input the third term of the infinite geometric series: " << endl;
cin >> c;
d = abs(c/b);
e = 1 - d;
if(d > 1){
cout << "Diverges!" << endl;
}
else{
f = a/e;
cout << "The sum of this infinite geometric sequence is " << f << endl;
}
exit();
}

int exit() {
char g;
cout << "Would you like to find another sequence? (y or n)" << endl;
cin >> g;
if(g == 'y'Winky Winky {
main();
}
else if (g=='n'Winky Winky {
return 0;
}
else return 0;
}
Last edited by 3arc ; 01-28-2011 at 06:17 AM.

The following user thanked 3arc for this useful post:

fightinillini94
01-28-2011, 09:26 PM #7
fightinillini94
Climbing up the ladder
Originally posted by 3arc View Post
No, what OP is doing is C++.

@OP: I fixed up your code, but for ease of use I added a second function to handle exiting the program, I also had to prototype that function.

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

int exit();

int main() {
double a;
double b;
double c;
double d;
double e;
double f;

cout << "Welcome to Justin's Infinite Geometric Solver!" << endl;
cout << "Input the first term of the infinite geometric series: " << endl;
cin >> a;
cout << "Input the second term of the infinite geometric series: " << endl;
cin >> b;
cout << "Input the third term of the infinite geometric series: " << endl;
cin >> c;
d = abs(c/b);
e = 1 - d;
if(d > 1){
cout << "Diverges!" << endl;
}
else{
f = a/e;
cout << "The sum of this infinite geometric sequence is " << f << endl;
}
exit();
}

int exit() {
char g;
cout << "Would you like to find another sequence? (y or n)" << endl;
cin >> g;
if(g == 'y'Winky Winky {
main();
}
else if (g=='n'Winky Winky {
return 0;
}
else return 0;
}


Thanks so much man. I also noticed you put "use namespace std" up top. You can put it there instead of your main function?
01-28-2011, 10:08 PM #8
3arc
Meow.
Originally posted by fightinillini94 View Post
Thanks so much man. I also noticed you put "use namespace std" up top. You can put it there instead of your main function?


You /could/ put it anywhere, but it's best to keep it at the top.

The following user thanked 3arc for this useful post:

fightinillini94
02-07-2011, 07:40 PM #9
Originally posted by fightinillini94 View Post
Thanks so much man. I also noticed you put "use namespace std" up top. You can put it there instead of your main function?


Originally posted by 3arc View Post
You /could/ put it anywhere, but it's best to keep it at the top.


using namespace std; only applies to where you put it, if you put it at the top it becomes global, say you put it in

int main()
{
using namespace std;
}

it only applies inside int main()

The following user thanked TheUberFail for this useful post:

fightinillini94

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo