How to get back to switch in c++ - c++

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.

Related

How to remove Warnings From our Code in C++? [duplicate]

This question already has answers here:
Why does flowing off the end of a non-void function without returning a value not produce a compiler error?
(11 answers)
Variable length arrays (VLA) in C and C++
(5 answers)
Closed 6 months ago.
What does " control reaches end of non-void function" means??
How to remove the warnings from out code ?
#include <bits/stdc++.h>
using namespace std;
int LinearSearch()
{
int ans=-1;
cout << "Enter the Size of the array: \n";
int n;
cin >> n;
cout << "Enter the array elements: \n";
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int key;
cout << "Enter the key: \n";
cin >> key;
for (int i = 0; i < n; i++)
{
if (arr[i] == key)
{
cout << "the " << key << " is found at index " << i << endl;
}
return ans;
}
}
int main()
{
while (1)
{
cout << "\t Main Menu\n";
cout << "1. For Linear Search\n";
cout << "2. For Binary Search\n";
cout << "3. For First and last Occurence\n";
int ch;
cout << "Enter the choice: \n";
cin >> ch;
switch (ch)
{
case 1:
cout<< LinearSearch();
break;
case 2:
break;
case 3:
break;
default:
cout << "Invalid Choice OOps!! ";
break;
}
}
return 0;
}
enter image description here
AS I am trying to run it it is giving me Warning Why??
Error is: warning: control reaches end of non-void function [-Wreturn-type]
How to resolve it?
When n is zero, you never reach a return. You need a return after the loop. Returning -1 might be the sensible choice in that situation. (Also, the return in the loop is misplaced...)
It means that int LinearSearch() is expected to return an int but there are code paths where that does not happen. For instance, if n == 0. You fix this by adding a return statement with an appropriate value on that code path. It's probably an error that you have the return within the for() loop as this means you get at most one iteration. Maybe this is what you want?
int LinearSearch()
{
int ans=-1;
cout << "Enter the Size of the array: \n";
int n;
cin >> n;
cout << "Enter the array elements: \n";
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int key;
cout << "Enter the key: \n";
cin >> key;
for (int i = 0; i < n; i++)
{
if (arr[i] == key)
{
cout << "the " << key << " is found at index " << i << endl;
ans = i;
break;
}
}
return ans;
}
Like others have mentioned, the warning indicates one of your code path has no return value. In LinearSearch after the for loop a return is missing. You can use -1 to return when a key match is not found or better still is if your C++ compiler supports C++17 or higher standard then I would suggest using std::optional and to return a "no value" to use std::nullopt and return the "ans" when you actually find the key.
Please look at the code below for a sample implementation.
#include <optional>
using namespace std;
std::optional<int> LinearSearch()
{
int ans;
cout << "Enter the Size of the array: \n";
int n;
cin >> n;
cout << "Enter the array elements: \n";
int arr[n];
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
int key;
cout << "Enter the key: \n";
cin >> key;
for (int i = 0; i < n; i++)
{
if (arr[i] == key)
{
cout << "the " << key << " is found at index " << i << endl;
return ans;
}
}
return std::nullopt;
}
int main()
{
while (1)
{
cout << "\t Main Menu\n";
cout << "1. For Linear Search\n";
cout << "2. For Binary Search\n";
cout << "3. For First and last Occurence\n";
int ch;
cout << "Enter the choice: \n";
cin >> ch;
switch (ch)
{
case 1:{
auto res = LinearSearch();
if (res) cout<< *res;
else cout << "key not found";
}
break;
case 2:
break;
case 3:
break;
default:
cout << "Invalid Choice OOps!! ";
break;
}
}
return 0;
}

I want following c++ program of bank account to run for 5 customers.But its saying that my object array has incomplete type

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;
}

How to put conditions in char array?

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;
}

How can I end a do-while cycle?

Im new at programming and for homework, my teacher ask me to make an option menu that does different things, but I have a problem, case 5 is supposed to end the program, but if I select 5, the do-while cycle keeps asking me if I want to do something else, when I need that if I choose 5, the program ends, how can I end with the cycle and put the option to exit the program?
Thanks, and any help is welcome
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <conio.h>
using namespace std;
int main() {
string name, lname;
int an, result;
int num;
char option;
int n, x;
time_t t, b;
char* f;
int flag = 0;
int i = 0;
do {
cout << "Option Menu";
cout << "\n1) Name and your last name";
cout << "\n2) Years of life";
cout << "\n3) First 100 numbers divisible by 3";
cout << "\n4) Date and hour";
cout << "\n5) Exit\n";
cin >> num;
switch (num) {
case 1:
cout << "\nWrite your name: ";
cin >> name;
cout << "\nWrite your last name: ";
cin >> lname;
cout << "\nYour complete name is: " << name << " " << lname;
break;
case 2:
cout << "\nWhat year you were born?: ";
cin >> an;
result = 2019 - an;
cout << "\nYou have " << result << " years\n";
break;
case 3:
for (i = 0; flag < 100; i++) {
if (i % 3 == 0) {
cout << i << "\n";
flag++;
}
}
break;
case 4:
b = time(&t);
f = ctime(&b);
printf("%s\n", f);
getch();
break;
}
cout << "\n Do you want to do something else?: ";
cin >> option;
} while (option == 's' or option == 'S');
cout << "\nGood bye :)" << endl;
system("pause");
return 0;
}
It's probably easiest to just simply use the if statement to check if num == 5 outside the switch statement and break the while loop when num is equal to 5.
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <conio.h>
using namespace std;
int main() {
string name, lname;
int an, result;
int num;
char option;
int n, x;
time_t t, b;
char* f;
int flag = 0;
int i = 0;
do {
cout << "Option Menu";
cout << "\n1) Name and your last name";
cout << "\n2) Years of life";
cout << "\n3) First 100 numbers divisible by 3";
cout << "\n4) Date and hour";
cout << "\n5) Exit\n";
cin >> num;
switch (num) {
case 1:
cout << "\nWrite your name: ";
cin >> name;
cout << "\nWrite your last name: ";
cin >> lname;
cout << "\nYour complete name is: " << name << " " << lname;
break;
case 2:
cout << "\nWhat year you were born?: ";
cin >> an;
result = 2019 - an;
cout << "\nYou have " << result << " years\n";
break;
case 3:
for (i = 0; flag < 100; i++) {
if (i % 3 == 0) {
cout << i << "\n";
flag++;
}
}
break;
case 4:
b = time(&t);
f = ctime(&b);
printf("%s\n", f);
getch();
break;
}
if (num == 5) {
break;
}
cout << "\n Do you want to do something else?: ";
cin >> option;
} while (option == 's' || option == 'S');
cout << "\nGood bye :)" << endl;
system("pause");
return 0;
}
To accomplish this task, your switch must have either a case 5: or a default: block which assigns a value other than s or S to the option variable. That will allow your program to break out of the do..while loop.
There is another problem though. The user will still be asked if he wants to do something else even if they chose the Exit option. To avoid this, you can use an extra variable (for example, a userExits boolean) to skip the part where the program asks for input after the switch.
Here is a possible solution:
// after all the variable declarations
bool userExits = false;
do {
// All menu options
cin >> num;
switch (num) {
// (...)
case 4:
b = time(&t);
f = ctime(&b);
printf("%s\n", f);
getch();
break;
case 5:
userExits = true;
break;
}
if (userExits){
option = "exit";
}
else{
cout << "\n Do you want to do something else?: ";
cin >> option;
}
} while (option == 's' or option == 'S');
cout << "\nGood bye :)" << endl;

Why do I keep getting "error: expected unqualified-id before 'case'"?

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.