Hello so i have this char array which needs to have some conditions in case 1 and 2. v[0] must be between 0 and 19 and v[0]+v[1] must be <= 19. I cannot figure it out how to do it. Also in case 1 if i have n=2 and the numbers are 2 and 3 i need to get 2 3 4.
#include <iostream>
using namespace std;
int main()
{
char v[20];
int pc, functie, n, i, loop;
do {
cout << "Este pornit calculatorul? (0/1) ";
cin >> pc;
} while (pc != 1);
loop:
cout << "Ce functie selectati? 1-4 ";
cin >> functie;
switch (functie) {
case 1:
cout << "Citire din memorie" << endl;
cout << "Cate numere cititi din memorie: ";
cin >> n;
for (i = 0; i < n; i++) {
cout << "v[" << i << "]=" << v[i] << " ";
cout << endl;
}
break;
case 2:
cout << "Scriere in memorie" << endl;
cout << "Cate numere scrieti ";
cin >> n;
for (i = 0; i < n; i++) {
cout << "v[" << i << "]= ";
cin >> v[i];
}
break;
case 3:
cout << "Golirea memoriei";
for (i = 0; i < n; i++)
v[i] = '\0';
cout << endl;
break;
case 4:
cout << "Oprirea calculatorului";
exit(0);
break;
default:
cout << "Nu ati selectat nici o functie";
}
goto loop;
}
Related
I want the following C++ program of bank accounts to run for 5 customers. But it's saying that my object array has an incomplete type. This code works ok for 1 customer, but it cannot work for more than one customer.
// bank account
#include <iostream>
using namespace std;
class BA
{
// BA C[2];
string name;
int acc_no;
string acc_type;
int balance;
public:
int getdata();
void deposit()
{
int a;
cout << "How much you want to deposit: ";
cin >> a;
balance += a;
}
void withdraw()
{
int w;
cout << "How much you want to withdraw: ";
cin >> w;
balance -= w;
}
void display()
{
for (int i = 0; i < 10; i++)
{
cout << "Name: " << name << endl;
cout << "Account no. : " << acc_no << endl;
cout << "Account type: " << acc_type << endl;
cout << "Your current balance: " << balance << endl;
}
};
BA C[2];
int BA ::getdata()
{
for (int i = 0; i < 2; i++)
{
cout << "Enter your name: " << i;
cin >> C[i].name;
cout << "Enter your acc_no: " << i;
cin >> C[i].acc_no;
cout << "Enter your acc_type: " << i;
cin >> C[i].acc_type;
cout << "Enter your balance: " << i;
cin >> C[i].balance;
}
}
int main()
{
BA C[2];
int x;
// for(int i=0;i<10;i++){
do
{
cout << endl
<< "To enter your Bank Information press 1: " << endl;
cout << "To deposit an amount in your bank press 2: " << endl;
cout << "To withdraw an amount from your bank press 3:" << endl;
cout << "To view your current balance and bank info press 4:" << endl;
cout << "To quit press 5:" << endl;
cout << "\n\twhat is your option: ";
cin >> x;
switch (x)
// for(int i=0;i<10;i++){
{
case 1:
for (int i = 0; i < 2; i++)
{
C[i].getdata();
}
break;
case 2:
for (int i = 0; i < 2; i++)
C[i].deposit();
break;
case 3:
for (int i = 0; i < 2; i++)
C[i].withdraw();
break;
case 4:
for (int i = 0; i < 2; i++)
C[i].display();
break;
case 5:
break;
default:
cout << "Error input try again:" << endl;
}
} while (x != 5);
return 0;
}
Hello I am trying to go back to switch after every case and I can't figure it out. I've tried with return but I can return just to main and it doesn't save the values from v[i]. Here is the code. The code itself should represent how RAM works on 8 bit. Also, v[0] should be between 0 and 19 and v[0]+v[1] should be less than 19 and I don't know how to implement that.
#include < iostream >
using namespace std;
int main() {
char v[20];
int pc,
functie,
n,
i,
loop;
do {
cout << "Este pornit calculatorul? (0/1) ";
cin >> pc;
}
while ( pc != 1 );
cout << "Ce functie selectati? 1-4 ";
cin >> functie;
switch (functie) {
case 1:
cout << "Citire din memorie" << endl;
cout << "Cate numere cititi din memorie: ";
cin >> n;
for (i = 0; i < n; i++) {
cout << v[i];
}
break;
case 2:
cout << "Scriere in memorie" << endl;
cout << "Cate numere scrieti ";
cin >> n;
for (i = 0; i < n; i++) {
cout << "v[" << i << "]= ";
cin >> v[i];
}
break;
case 3:
cout << "Golirea memoriei";
for (i = 0; i < n; i++)
v[i] = '\0';
break;
case 4:
cout << "Oprirea calculatorului";
exit(0);
break;
default:
cout << "Nu ati selectat nici o functie";
}
return 0;
}
The switch should be included in a loop if you want to iterate over it multiple times.
Also you should not generally include vulgar language in your code, even if it is not in English (6th variable you declared).
Update: I managed to do the code but i still need to have v[0] between 0 and 19 and v[0]+v[1] <=19 and i don't know how to do it.
#include <iostream>
using namespace std;
int main() {
char v[20];
int pc, functie, n, i,loop;
do {
cout << "Este pornit calculatorul? (0/1) ";
cin >> pc;
}
while (pc != 1);
while(loop=1)
{
cout << "Ce functie selectati? 1-4 ";
cin >> functie;
switch (functie) {
case 1:
cout << "Citire din memorie" << endl;
cout << "Cate numere cititi din memorie: ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "v[" << i << "]="<<v[i]<<" ";
cout<<endl;
}
break;
case 2:
cout << "Scriere in memorie"<<endl;
cout << "Cate numere scrieti ";
cin >> n;
for (i = 0; i < n; i++) {
cout << "v[" << i << "]= ";
cin >> v[i];
}
break;
case 3:
cout << "Golirea memoriei";
for(i=0;i<n;i++)
v[i]='\0';
cout<<endl;
break;
case 4:
cout << "Oprirea calculatorului";
exit(0);
break;
default:
cout << "Nu ati selectat nici o functie";
}
}
}
You can use goto to jump at starting point of switch or any function for sake of simplicity. Consider the following code:
loop:
switch(...) {
case ...
break;
}
goto loop;
Note that it's not always good to use the goto, it may throw serious problems in programs in some situations. Alternatively, you can do the same with for in a very convenient way. The example:
for(; ;)
{
// body of the for loop.
}
Hope you understand.
I'm making a code where the users decide which shape they want to see using a switch statement. I was doing fine with the first four cases but I ran into trouble when I got to the fifth one. I Keep getting an error saying "error: expected unqualified-id before 'case' " whenever I try to make a fifth case and I can't figure out the issue.
What is happening with my code?
#include <iostream>
using namespace std;
int main() {
int choice;
cout << "Please enter the shape you would like to see" << endl;
cout << "1. Rectangle" << endl;
cout << "2. Square" << endl;
cout << "3. Right Triangle" << endl;
cout << "4. Isosceles Triangle" << endl;
cout << "5. Kite" << endl;
cout << "6. Quit" << endl;
cin >> choice;
switch(choice){
case 1 : double length,width; //rectangle
cout << "Enter the length and width of the rectangle" << endl;
cin >> length >> width;
for(int i = 0; i < length; i++){
for(int j = 0; j < width; j++){
cout << "*";
}
cout << "\n";
}
break;
case 2 : cout << "Enter the length and width of the square" << endl;
//square
cin >> length >> width;
for(int i = 0; i < length; i++){
for(int j = 0; j < width; j++){
cout << " * ";
}
cout << "\n";
}
break;
case 3 : cout << "Enter the length of the triangle" << endl;
//right triangle
cin >> length;
for(int i = 0; i < length; i++){
for(int j = 0; j < i; j++){
cout << "*";
}
cout << "\n";
}
break;
case 4 : int star=1;
for(int i=1;i<=5;i++){
for(int j=4;j>=i;j--){
cout<<" ";
}
for(int z=0;z<star;z++){
cout<<"*";
}
cout<<endl;
star=star+2;
}
}
}
case 5 : cout << "???";
Your curly braces are mismatched. You have two extra } before case 5.
These braces should go after case 5. One for the switch statement, one for the main function.
In addition, your #include on the first line should not have any indentation.
So for my program of manipulating a vector, a choice is presented at the end of each switch case: If the letter 'q' is entered, the program is supposed to exit the while loop and end the program, but when I enter the letter 'q', the program crashes instead of exiting properly. Why is this happening? Is it an infinite loop?
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int rnum;
int jnum;
int i;
int lim = 5;
char choice='b';
vector<int> jersey(lim);
vector<int> rating(lim);
cout << "MENU" << endl;
cout << "a - Add player" << endl;
cout << "d - Remove player" << endl;
cout << "u - Update player rating" << endl;
cout << "r - Output players above a rating" << endl;
cout << "o - Output roster" << endl;
cout << "q - Quit" << endl;
cout << "" << endl;
cout << "Choose an option:" << endl;
cin >> choice;
while(choice != 'q') {
switch(choice) {
case 'a' :
// addplayer
for(int i = 0; i<=lim-1; i++)
{
cout << "Enter a new player's jersey number:" << endl;
cin >> jersey.at(i);
cout <<"Enter the player's rating:" << endl;
cin >> rating.at(i);
}
cout << "Choose an option:" << endl;
cin >> choice;
break;
case 'u' :
// updat rating
cout << "Enter a jersey number:" << endl;
cin >> jnum;
for( int i = 0; i <= lim-1; i++ )
{
if( jersey.at(i) == jnum )
{
cout << "Enter a new rating for player:" <<endl;
cin >> rnum;
rating.at(i) = rnum;
break;
}
}
cout << "Choose an option:" << endl;
cin >> choice;
break;
case 'o':
cout << "ROSTER" << endl;
for(int i = 0; i<lim; i++)
{
cout << "Player "<<i+1 <<" -- Jersey number:" << " " <<jersey.at(i) << ", " << "Rating: " << rating.at(i) << endl;
}
cout << "Choose an option:" << endl;
cin >> choice;
break;
case 'd':
cout << "Enter a jersey number:" << endl;
cin >> jnum;
for( std::vector<int>::iterator spot = jersey.begin(); spot != jersey.end(); ++spot )
{
if( *spot == jnum )
{
jersey.erase( spot );
rating.erase( spot );
lim = jersey.size();
}
}
cout << "Choose an option:" << endl;
cin >> choice;
break;
case 'r':
cout << "Enter a rating:" << endl;
cin >> rnum;
for( int i = 0; i <= lim-1; i++ )
{
if( rating.at(i) >= rnum )
{
cout << "Player "<<i+1 <<" -- Jersey number:" << " " <<jersey.at(i) << ", " << "Rating: " << rating.at(i) << endl;
}
}
cout << "Choose an option:" << endl;
cin >> choice;
break;
default:
cout << "Choose an option:" << endl;
cin >> choice;
break;
}
}
return 0;
}
Your case 'd' looks incorrect. You can't use spot to erase an element from rating since spot only iterates over jersey's elements. To fix this, and potentially solve your crash or infinite loop issue, you can replace the following code:
if( *spot == jnum )
{
jersey.erase( spot );
rating.erase( spot );
lim = jersey.size();
}
With:
if( *spot == jnum )
{
jersey.erase( spot );
rating.erase( rating.begin() + (spot - jersey.begin()) );
lim = jersey.size();
}
Here, spot - jersey.begin() will give you the index of the jersey you just found, adding that to rating.begin() will therefore give you the corresponding rating and allow you to properly erase it from the rating vector.
In addition, since your code allows duplicate jersey numbers, deleting a duplicate won't always delete all instances of the number. If you want to fix this, you can add spot--; after lim = jersey.size(); to ensure you don't skip over duplicate elements.
How to get rid of goto statement in this program and end the flip:>>>?
int main()
{
int array[10];
int sum = 0;
int desending;
int mul = 1;
float avg = 10;
int option;
do
{
flip1:
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
flip2:
if (option == 1)
{
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
}
else if (option == 2)
{
int array[10], o, index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
}
else if (option == 3)
{
system("cls");
int a[10], r, t, temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
for (t = 0; t<9; t++)
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
}
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
char q;
cin >> q;
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
} while (option != 4);
return 0;
}
You can refer the below code.
Before entering the loop.
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
do {
if (option == 1)
{
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
/* All text as same */
At the end: change
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
To.
if (q == 'e')
{
return 0;
}
system("cls");
if (q == 'm')
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
}
PS: You can also create a function that prints the menu, get input from user and return the same.
If you divide your code into chunks of cohesive functionality, you can put them into their own functions and the main function can be much simpler. Here's my recommendation:
#include <iostream>
#include <cstdlib>
using namespace std;
int getOption1()
{
int option;
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
return option;
}
char getOption2()
{
char option;
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
cin >> option;
return option;
}
void doArrayBasics()
{
int array[10];
int sum = 0;
float avg = 10;
int mul = 1;
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
}
void doArraySearching()
{
int array[10];
int o;
int index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
}
void doArraySorting()
{
system("cls");
int a[10];
int r;
int t;
int temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
for (t = 0; t<9; t++)
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
}
int main()
{
int option1;
char option2 = 'r';
do
{
if ( option2 == 'r' )
{
option1 = getOption1();
}
switch (option1)
{
case 1:
doArrayBasics();
break;
case 2:
doArraySearching();
break;
case 3:
doArraySorting();
break;
case 4:
break;
default:
cout << "Invalid option " << option1 << endl;
}
if ( option1 != 4 )
{
option2 = getOption2();
}
} while (option1 != 4 && option2 != 'e' );
return 0;
}
You can use an inner loop to remove all gotos.
Remove flip1:, change flip2: to do {. The loop should begin like that:
do
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
do
{
if (option == 1)
{
At the end of the loop, change:
if (q == 'm')
{
system("cls");
goto flip1;
}
if (q == 'r')
{
system("cls");
goto flip2;
}
if (q == 'e')
{
return 0;
}
to :
if (q == 'e')
{
return 0;
}
system("cls");
} while (q == 'r');
Breaking your code into functions will also make it more readable but is not mandatory to remove gotos.
caveat: the following code not tested
int main()
{
int array[10];
int sum = 0;
int desending;
int mul = 1;
float avg = 10;
int option;
int done = 0;
int looping = 1;
while( !done )
{
cout << "\n\t\t------------------------------------------------" << endl;
cout << "\t\tlllllllll Menue lllllllllll" << endl;
cout << "\t\tlllllllll 1. Array Basics lllllllllll" << endl;
cout << "\t\tlllllllll 2. Searching Array lllllllllll" << endl;
cout << "\t\tlllllllll 3. Sorting Array lllllllllll" << endl;
cout << "\t\tlllllllll 4. Exit lllllllllll" << endl;
cout << "\t\t------------------------------------------------\n" << endl;
cout << "Please select an option" << endl;
cin >> option;
system("cls");
while( looping )
{
looping = 0;
switch( option )
{
case 1:
int h = 1;
int i;
for (i = 0; i < 10; i++)
{
cout << h << " Enter the Numbers ";
cin >> array[i];
sum = sum + array[i];
mul = mul*array[i];
h++;
}
system("cls");
for (int j = 0; j < 10; j++)
{
cout << "Given numbers" << j << " = " << array[j] << "\n";
}
avg = sum / avg;
int max = array[0];
int min = array[0];
for (int i = 0; i < 10; i++)
{
if (array[i] > max)
{
max = array[i];
}
else if (array[i] < min)
{
min = array[i];
}
}
cout << "\nSum of the Numbers= 4" << sum << endl;
cout << "\nProduct of the Numbers is = " << mul << endl;
cout << "\nThe Avg of Numbers is = " << avg << endl;
cout << "\nmaximum of the Numbers is = " << max << endl;
cout << "\nminimum of the Numbers is = " << min << endl;
break;
case 2:
int array[10], o, index = -1;
cout << "enter the elements of array" << endl;
for (o = 0; o < 10; o++)
{
cin >> array[o];
}
system("cls");
int p;
cout << "enter value to find" << endl;
cin >> p;
for (int c = 0; c < 10; c++)
{
if (array[c] == p)
index = c;
}
system("cls");
if (index == -1)
{
cout << "no value found" << endl;
}
else
cout << "Value found at\t" << index << endl;
break;
case 3:
system("cls");
int a[10], r, t, temp;
cout << "Enter the array elements: " << endl;
for (r = 0; r<10; ++r)
cin >> a[r];
system("cls");
for (r = 0; r<10; ++r)
{
for (t = 0; t<9; t++)
{
if (a[r]<a[t])
{
temp = a[r];
a[r] = a[t];
a[t] = temp;
}
}
}
cout << "Array after sorting: " << endl;
for (r = 0; r<10; r++)
cout << a[r] << "\t ";
break;
case 4:
done = 1;
break;
default:
cout << "\nplease select an option\n" << endl;
cout << "press m for main menue" << endl;
cout << "press r for repeat" << endl;
cout << "press e for exit" << endl;
char q;
cin >> q;
switch( q )
{
case 'm':
system("cls");
// looping already set = 0
break;
case 'r':
system("cls");
looping = 1;
break;
case 'e':
done = 1;
break;
default:
break;
} // end switch
break;
} // end switch
} // end while looping
} // end while not done
return 0;
} // end function: main