Hello it is my first time here and I just started C++ so hopefully my question isn't too dumb or anything:

I was writing this thing that can do geometric series and arithmetic series. I can write the geometric series code alright and the arithmetic series code alright but once I put them together into one package where I can allow the user to choose which type or series they want the program to solve, errors started to occur. Below is my code for the combined package

// geometric and aritmetric series calculator

#include <iostream>
#include <math.h>

using namespace std;

int main()
{

char operation;
cout << "press 1 for geometric series and 2 for aritmetric series" << endl;
cin >> operation;

switch(operation)
{

case '1':
           double total1=0, t, u1, k, r;

cout << "This program finds the sum of any arithmetric series" << endl;
cout << "Please insert the value of the frist term:" << endl;
cin >> u1;
cout << "Please insert the value of the ratio" << endl;
cin >> r;
cout << "Please insert the amount of terms you wish to sum" << endl;
cin >> t;
cout << "Below is the results of the series, each row is the sum of one extra term" << endl;
cout << "The last result is the sum of this series with the specified amount of terms" << endl;

if (r == 1)
{
cout << "Sorry, this cannot compute the case of the ratio equals to 1" << endl;
}

while (k !=t)
    {
    for (double k=1; k<=t; k++)
    {
    total1 = ((u1*(1-pow(r,k)))/(1-r));
    cout << total1 << endl;
    usleep(100000);

     if (k == t)
     {
     cout << "End of series, your final result is shown as the last number displayed" << endl;
     return 0;
     }
    }
  }
       break;

case '2':
           double total=0, x, a1, n, d;

cout << "This program finds the sum of any arithmetric series" << endl;
cout << "Please insert the value of the frist term:" << endl;
cin >> a1;
cout << "Please insert the value of the difference" << endl;
cin >> d;
cout << "Please insert the amount of terms you wish to sum" << endl;
cin >> x;
cout << "Below is the results of the series, each row is the sum of one extra term" << endl;
cout << "The last result is the sum of this series with the specified amount of terms" << endl;

while (n !=x)
    {
    for (double n=1; n<=x; n++)
    {
    total = ((n/2)*(2*a1+(n-1)*d));
    cout << total << endl;
    usleep(100000);

     if (n == x)
     {
     cout << "End of series, your final result is shown as the last number displayed" << endl;
     return 0;
     }
    }
  }
       break;

default:
       cout << "Invalid input, please try again" << endl;
       return -1;
}
       return 0;
}

It gave me compiling errors that said:

~/Desktop/C++/session_2$ g++ geoariplus.cc -o geoariplus
geoariplus.cc: In function ‘int main()’:
geoariplus.cc:53: error: jump to case label
geoariplus.cc:19: error: crosses initialization of ‘double total1’
geoariplus.cc:83: error: jump to case label
geoariplus.cc:54: error: crosses initialization of ‘double total’
geoariplus.cc:19: error: crosses initialization of ‘double total1’


Below is my code for only the geometric which worked fine by itself:

// print geoplus

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    double total=0, x, a1, n, d;

cout << "This program finds the sum of any arithmetric series" << endl;
cout << "Please insert the value of the frist term:" << endl;
cin >> a1;
cout << "Please insert the value of the ratio" << endl;
cin >> d;
cout << "Please insert the amount of terms you wish to sum" << endl;
cin >> x;
cout << "Below is the results of the series, each row is the sum of one extra term" << endl;
cout << "The last result is the sum of this series with the specified amount of terms" << endl;

if (d == 1)
{
cout << "Sorry, this cannot compute the case of the ratio equals to 1" << endl;
}

while (n !=x)
    {
    for (double n=1; n<=x; n++)
    {
    total = ((a1*(1-pow(d,n)))/(1-d));
    cout << total << endl;
    usleep(100000);

     if (n == x)
     {
     cout << "End of series, your final result is shown as the last number displayed" << endl;
     return 0;
     }
    }
  }
}

Below is my code to only the arithmetric series which also works fine by itself:

// print arithmetric adv

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    double total=0, x, a1, n, d;

cout << "This program finds the sum of any arithmetric series" << endl;
cout << "Please insert the value of the frist term:" << endl;
cin >> a1;
cout << "Please insert the value of the difference" << endl;
cin >> d;
cout << "Please insert the amount of terms you wish to sum" << endl;
cin >> x;
cout << "Below is the results of the series, each row is the sum of one extra term" << endl;
cout << "The last result is the sum of this series with the specified amount of terms" << endl;

while (n !=x)
    {
    for (double n=1; n<=x; n++)
    {
    total = ((n/2)*(2*a1+(n-1)*d));
    cout << total << endl;
    usleep(100000);

     if (n == x)
     {
     cout << "End of series, your final result is shown as the last number displayed" << endl;
     return 0;
     }
    }
  }
}

So if you guys can help me spot what I did wrong when combining them, that would be great.

P.S. - I know I pasted quite a lot of code, hope this doesn't annoy the community here.

create your case like this

case '1':
{
//put code here
}

Use braces so you can define variables in your case.

Edited 5 Years Ago by gerard4143: n/a

It might behoove you to break that functionality into separate functions. For instance

case '1':
   do_stuff_1_here ();
   break;
case '2':
   do_stuff_2_here ();
   break;

It helps maintain readability and understanding.

You also may want to make functions for each of the cases.

case '1':
      first_function(Parameter_list);
      break;
case '2':
      second_function(Parameter_list);
      break;
default:
      break;
}

void first_function(Parameter_list){
<code>
}

void second_function(Parameter_list){
<code>
}

makes it cleaner, you can also use this code outside of the switch statement.

This question has already been answered. Start a new discussion instead.