0

I'm having problem in printing the case 3 of my menu because it's a problem that involves strings and work with char and the others cases works fine with vectors, how can I fix that?

#include <windows.h> //Lots of useful things
#include <iostream> //"cout" and "cin"
#include <conio.h> //"getch()" and "kbhit()"
#include <string> //Strings, obviously
#include <ctime> //Is needed in most compilers for the rand() seeds
#include <stdio.h>
#include <stdlib.h>

using namespace std; //I hate using std:: before every cout or string

int main(int argc, char** argv) {

    int option;
    int arr[10], sum, i, j, n, c,k,l;
    char cadena;
    char *ptrcad;
    int num, c1,d1,a[100],b[100];

    cout<<" MENU DE OPCIONES: "<<endl<<endl;
    cout<<" 1. Calcular la suma de todos los elementos de un vector"<<endl;
    cout<<" 2. Indicar si un valor num se encuentra en un vector"<<endl;
    cout<<" 3. Invertir una cadena apuntada"<<endl;
    cout<<" 4. Invertir los elementos de un vector vec de N posiciones"<<endl;
    cout<<" 5. Informar Ia posicion donde aparece por ultima vez un valor"<<endl;
    cout<<" 6. Determinar si dos vectores son iguales"<<endl;
    cout<<" 7. Eliminar de una lista encadenada apuntada por cad todas la apariciones"<<endl;
    cout<<" 8. Elabore una rutina recursiva que imprima los elementos de una lista encadenada"<<endl;
    cout<<" 9. Elaborar una rutina recursiva que permita ordenar un vector de forma ascendente"<<endl;
    cout<<" 10. Salir"<<endl<<endl;
    cout<<" ELIJA UNA OPCION: ";
    cin>>option;

    system("cls");//Clear the screen and start the second part of the code

    switch(option){
    case 1:
        sum=0;
        for(i=0;i<10;i++){
            cout<<"Ingrese dato: ";
            cin>>arr[i];
            sum+=arr[i];
        }
        cout<<"\n Datos almacenados dentro del arreglo: "<<endl;
        for(j=0; j<10;j++){
            cout<<"  "<<arr[j]<<"    ";
        }
        cout<<"\n\n La suma total es = "<<sum<<endl;
        break;
    case 2:
        c=0;
        for(i=0;i<10;i++){
            cout<<"Ingrese dato: ";
            cin>>arr[i];
        }
        cout<<"\n Datos almacenados dentro del arreglo: "<<endl;
        for(j=0; j<10;j++){
            cout<<"  "<<arr[j]<<"    ";
        }
        cout<<"\n\n Ingrese el dato a verificar: ";
        cin>>n;
        for(i=0;i<10;i++){
            if(n==arr[i]){
                cout<<"El elemento "<<n<<" se encuentra en el arreglo en la posicion "<<i<<endl;}
            else{
                c++;}
         }
         if(c++==10){
         cout<<"El elemento "<<n<<" no se encuentra en el arreglo"<<endl;}
        break;
     case 3:
        char cad[20];
        char *ptrcad; //puntero que apunta a mi cadena
        cout<<"Ingrese la cadena: "<<endl;
        gets(cad);
        cout<<cad;
        for(int i=5;i>=0;i--){
            ptrcad=&cad[i];//asigno puntero, ptr cad apunta al contenido de cad
            cout<<*ptrcad;
            }
        break;
    case 4:
        cout<<"Ingrese numero de elementos del arreglo: ";
        cin>>num;
        for(i=0;i<num;i++){
            cout<<"Ingrese dato: ";
            cin>>arr[i];
        }
        cout<<"\n Datos almacenados dentro del arreglo: "<<endl;
        for(j=0; j<num;j++){
            cout<<"  "<<arr[j]<<"    ";
        }
        for (int i=0; i<num/2; i++){
        int temp=arr[i];
        arr[i]=arr[num-1-i];
        arr[num-1-i]=temp; }
        cout<<"\n El arreglo invertido es: "<<endl;
        for (int i=0; i<num; i++){
            cout<<"  "<<arr[i]<<"    "; }
        break;
    case 5:
        break;
    case 6:
        int v1[5],v2[5], ban;
        ban=0;
        for(i=0;i<5;i++){
            cout<<"Ingrese dato vector 1: ";
            cin>>v1[i];
        }
        cout<<"\n Datos almacenados dentro del arreglo 1: "<<endl<<endl;
        for(j=0; j<5;j++){
            cout<<"  "<<v1[j]<<"    ";
        }
                for(i=0;i<5;i++){
            cout<<"Ingrese dato vector 2: ";
            cin>>v2[i];
        }
        cout<<"\n Datos almacenados dentro del arreglo 2: "<<endl<<endl;
        for(j=0; j<5;j++){
            cout<<"  "<<v2[j]<<"    ";
        }
        for(i=0;i<5;i++)
        if(v1[i]==v2[i])
        ban++;
        if(ban==5)
        cout<<"\nLos vectores son iguales"<<endl;
        else
        cout<<"\nLos vectores no son iguales"<<endl;
        break;
    case 7:
        break;
    case 8:
        break;
    case 9:
        int a[5], aux;
        for(i=0;i<5;i++){
            cout<<"Ingrese dato vector 1: ";
            cin>>a[i];
        }
        cout<<"\n Datos almacenados dentro del arreglo 1: "<<endl<<endl;
        for(j=0; j<5;j++){
            cout<<"  "<<a[j]<<"    ";
        }
         for(i=0; i<=5;i++)
            for(l=i+1; l<=5;l++)
            if(a[i]>=a[l])
            {
                aux=a[l];
                a[l]=a[i];
                a[i]=aux;
            }
            cout<<"El vector ordenado es: ";
            for(i=0;i<=5;i++)
            cout<<a[i]<<"  ";
        break;
    case 10:
        exit(0);
    default: cout<<"Opcion no valida";
    }

    return 0;
}
NightShadeQueen
  • 3,284
  • 3
  • 24
  • 37
  • http://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used – Neil Kirk Sep 18 '15 at 02:23
  • 1
    You've included string header, why not use its contents? I bet many errors will just go away then. – Neil Kirk Sep 18 '15 at 02:23
  • What is the error you get? And what are trying to achieve in case 3? print user entered 6 characters in reverse? – ubi Sep 18 '15 at 02:25
  • In the case 3 I have to print a string in reverse using pointers, my error is that in the console I can't write anything and the program just exit – Brian Daniel García Sep 18 '15 at 02:27

1 Answers1

0

From your comments

In the case 3 I have to print a string in reverse using pointers, my error is that in the console I can't write anything and the program just exit

I don't see any problem with your program. It exits as expected after doing the action for the option you specify. If you want it to continue to accept options you need to have some sort of a loop eg.

while(true)
{
     switch(option)
     {
           case 0:
               // actions
           break;
           ....
           case 10:
               exit(0);
     }
}

Also, it is not generally recommended to use gets to read from stdin

Notice that gets is quite different from fgets: not only gets uses stdin as source, but it does not include the ending newline character in the resulting string and does not allow to specify a maximum size for str (which can lead to buffer overflows).

In fact, Microsoft C++ compiler throws an error if you use it. Suggest using cin or fgets instead.

ubi
  • 4,041
  • 3
  • 33
  • 50