how can i make the data that i input in a function appear in another one?

This is a discussion on how can i make the data that i input in a function appear in another one? within the C++ Programming forums, part of the General Programming Boards category; i got this problem, im doing this homework, i know you guy dont want to do this for me lol ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    8

    how can i make the data that i input in a function appear in another one?

    i got this problem, im doing this homework, i know you guy dont want to do this for me lol but yea, i already like "done" it, it runs, but see, the teacher said that he wanted us to us structures and functions on it, and i did but for some reason it doesnt do what its supposed to, it runs, but for example, i try to put in some data on the first case and when its finally done asking me for the information, it stops instead of sending me back to the menu, this is wat i got so far:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    #define CAP 10
    #define SEG 10
    #define COD 10
    #define COST 10
    #define PREC 10
    void ingresar();
    void revisar();
    main()
    {
     int op;
     int cap[CAP];
     int miliseg[SEG];
     float cost[COST];
     float prec[PREC];
     char codven[COD];
     printf("Programa para capturar datos de la compa&#241;ia de discos");
     printf("\n\nMenu de Opciones: ");
     printf("\n1. Ingresar un nuevo articulo al inventario.");
     printf("\n2. Revisar los datos del inventario.");
     printf("\n3. Salir del programa.");
     printf("\n\nFavor de elegir una opcion: ");
     scanf("&#37;d", &op);
     switch (op)
     {
      case 1:{
    	  ingresar();
    	  break;
              }
      case 2:{
    	  revisar();
    	  break;
              }
      default:{
    	   printf("\n\nOpcion incorrecta.");
    	   break;
    	   }
      }
     return 0;
    }
    
    
    void ingresar()
    {
     struct Discos
     {
      int op;
      int cap[CAP];
      int miliseg[SEG];
      float cost[COST];
      float prec[PREC];
      char codven[COD];
     } discos;
    
     int op;
      int cap[CAP];
      int miliseg[SEG];
      float cost[COST];
      float prec[PREC];
      char codven[COD];
    
     printf("\nFavor de ingresar los siguientes datos: ");
     printf("\n\n\nCapacidad del disco en byte: ");
     scanf("%d", &cap[1]);
     printf("\nTiempo de acceso en milisegundos: ");
     scanf("%d", &miliseg[1]);
     printf("\nCodigo del vendedor (A, B, C, D): ");
     scanf("%s", &codven[1]);
     printf("\nCosto de fabrica: ");
     scanf("%f", &cost[1]);
     printf("\nPrecio de venta al publico: ");
     scanf("%f", &prec[1]);
     return;
    }
    
    void revisar()
    {
     printf("\nDatos del inventario.");
     printf("\nCapacidad\tTiempo de acceso\tCodigo del vendedor\tCosto de fabrica\tPrecio de venta");
     printf("\n\nDiscos.cap\tDiscos.miliseg\tDiscos.codven\tDiscos.cost\tDiscos.prec");
     getch();
     return ;
    }


    i just need someone to tell me what i did wrong, theres no need for doing it for me, just tell me wat i did wrong and ill try to fix it, thanks


    by the way, sorry its in spanish, i think i forgot to mention some of the info in here, if i forgot to mention anything just tell me and ill try to see if i can fix it, thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Huh? What's it supposed to do? I don't understand anything since it's spanish.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    yea, sorry, its supposed to be a program to capture data for a disc or cd company, and it has a menu, thats why i used the switch.... case, and the first option is supposed to let me type in information about a list of disc, so that everytime that i type 1 in the menu its supposed to add another disc to the list, and the second option is meant to be to so that i can look at what i already got in the list, so since its a list, that why i used the struct for. im pretty new at this so im sorry if i didnt make sense

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    I'm still not sure, because I only see a function that inputs data and no function that outputs any of the inputted data.
    But what I can say is this: you do realize that you're using local variables, right? They will disappear when the function exits and can't be accessed from any other function.
    You also seem to have declared a struct, but you're not filling its member variables, but instead, you're filling extra copies local variables.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    I think you want a loop.

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    #define CAP 10
    #define SEG 10
    #define COD 10
    #define COST 10
    #define PREC 10
    void ingresar();
    void revisar();
    main()
    {
     int op;
     int cap[CAP];
     int miliseg[SEG];
     float cost[COST];
     float prec[PREC];
     char codven[COD];
     while( op != 3 ) // If they type 3 the program ends, I can't read spanish so I don't know what your 3 means
     {
     printf("Programa para capturar datos de la compaņia de discos");
     printf("\n\nMenu de Opciones: ");
     printf("\n1. Ingresar un nuevo articulo al inventario.");
     printf("\n2. Revisar los datos del inventario.");
     printf("\n3. Salir del programa.");
     printf("\n\nFavor de elegir una opcion: ");
     scanf("%d", &op);
     switch (op)
     {
      case 1:{
    	  ingresar();
    	  break;
              }
      case 2:{
    	  revisar();
    	  break;
              }
      default:{
    	   printf("\n\nOpcion incorrecta.");
    	   break;
    	   }
      }
      }
     return 0;
    }
    
    
    void ingresar()
    {
     struct Discos
     {
      int op;
      int cap[CAP];
      int miliseg[SEG];
      float cost[COST];
      float prec[PREC];
      char codven[COD];
     } discos;
    
     int op;
      int cap[CAP];
      int miliseg[SEG];
      float cost[COST];
      float prec[PREC];
      char codven[COD];
    
     printf("\nFavor de ingresar los siguientes datos: ");
     printf("\n\n\nCapacidad del disco en byte: ");
     scanf("%d", &cap[1]);
     printf("\nTiempo de acceso en milisegundos: ");
     scanf("%d", &miliseg[1]);
     printf("\nCodigo del vendedor (A, B, C, D): ");
     scanf("%s", &codven[1]);
     printf("\nCosto de fabrica: ");
     scanf("%f", &cost[1]);
     printf("\nPrecio de venta al publico: ");
     scanf("%f", &prec[1]);
     return;
    }
    
    void revisar()
    {
     printf("\nDatos del inventario.");
     printf("\nCapacidad\tTiempo de acceso\tCodigo del vendedor\tCosto de fabrica\tPrecio de venta");
     printf("\n\nDiscos.cap\tDiscos.miliseg\tDiscos.codven\tDiscos.cost\tDiscos.prec");
     getch();
     return ;
    }
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    op isn't even initialized, so you'll get a runtime error in later Visual Studio versions.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    how do you make functions that output data? i dont think the teacher ever thought us that lol is it by changing void into int? because i just tried that but its still does the same thing.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Uhh what? I don't understand what you mean, and I don't understand what your code is doing either.
    Do you mean return types? Such as
    Code:
    void revisar()
    to
    Code:
    int revisar()
    or something? If that's the case, then no.

    Maybe you could tell us what your code is trying to do. And maybe put english comments or so.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    thanks tjpanda, i forgot about that too! thanks

    ok ill try to put comment in english right now...

  10. #10
    Registered User
    Join Date
    Oct 2007
    Posts
    66
    Oh er well make the line int op = 0; then sorry didn't notice :x

    If by output data you mean to the screen it's just with printf, if you mean for it to take data and do something with it outside of it's own scope you can do this:

    Code:
    int functionwhatever(int whatever)
    {
    	// Does whatever
    	whatever += 1;
    	
    	return whatever;	// This line will "make" the value
    				// assigned to this function the value of "whatever"
    }
    
    int main()
    {
    	int j=0;
    	int k;
    	cin >> j;
    
    	k = functionwhatever(j);
    
    	cout << "Before the function: " << j << " After the function: " << k;
    	return 0;
    }
    So yeah you can just return values like that.


    edit:

    Another way if you have multiple variables you wish to change while in the scope of the function, which after looking at your code I think is what you want would be this:

    Code:
    void foo(int &i, int &k, int &j)
    {
    	// i k and j should be references
    	i += k;
    	k += (i-k);
    	j += j;
    }
    
    int main()
    {
    	int op1, op2, op3;
    	cout << "Input the three options:";
    	cin >> op1;
    	cin >> op2;
    	cin >> op3;
    
    	// do foo to them
    	foo(&op1, &op2, &op3);
    	cout << "\nOp1: " << op1; << " Op2: " << op2 << " Op3: " << op3;
    
    	return 0;
    }
    Last edited by tjpanda; 10-22-2007 at 01:27 PM.
    "When your work speaks for itself - don't interrupt!"

    -Samantha Ingraham.

  11. #11
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    #define CAP 10
    #define SEG 10
    #define COD 10
    #define COST 10
    #define PREC 10
    int ingresar(); //this is the function i did to input the data
    int revisar(); //this one is supposed to be the one to print it out
    main()
    {
     int op;
     int cap[CAP];
     int miliseg[SEG];
     float cost[COST];
     float prec[PREC];
     char codven[COD];
     while (op != 3)
     {
      printf("Programa para capturar datos de la compaņia de discos"); //tittle for the menu
      printf("\n\nMenu de Opciones: "); //menu
      printf("\n1. Ingresar un nuevo articulo al inventario."); //this option is to input the data
      printf("\n2. Revisar los datos del inventario."); //the one to check out what you already got
      printf("\n3. Salir del programa."); //exit
      printf("\n\nFavor de elegir una opcion: ");
      scanf("%d", &op); // this is when i save the option they choose from the menu
      switch (op)
      {
       case 1:{
      	   ingresar(); //first function for the first option
          	   break;
               }
       case 2:{
    	   revisar(); //second
    	   break;
               }
       default:{
    	    printf("\n\nFin del programa."); //save end of the program
    	    break;
    	    }
      }
     }
     return 0;
    }
    
    
    int ingresar()
    {
     struct Discos
     {
      int op;
      int cap[CAP];
      int miliseg[SEG];
      float cost[COST];
      float prec[PREC];
      char codven[COD];
     } discos;      //the struct to tell the function which variables im going to use in the variable discos
    
     int op;
      int cap[CAP];
      int miliseg[SEG];
      float cost[COST];
      float prec[PREC];
      char codven[COD]; //repeated the variables, i dont know why, the program gave me an error without them here
    
     printf("\nFavor de ingresar los siguientes datos: "); //ask to input the data
     printf("\n\n\nCapacidad del disco en byte: "); //its asking for how many bytes a disc can take
     scanf("%d", &cap[1]); //saves it in "cap" for capacity
     printf("\nTiempo de acceso en milisegundos: "); //readtime
     scanf("%d", &miliseg[1]);
     printf("\nCodigo del vendedor (A, B, C, D): "); //code of the vendor
     scanf("%s", &codven[1]);
     printf("\nCosto de fabrica: "); //costs from factory ??
     scanf("%f", &cost[1]);
     printf("\nPrecio de venta al publico: "); //price to the public
     scanf("%f", &prec[1]);
     return 0; //i thought this was so it would send me back to the menu, after this it doesnt work, and it doesnt send me back to the menu and saves the inputed data so i can look at it with the second option from the menu
    }
    
    
    
    int revisar() //it was to print out the inputted data
    {
     printf("\nDatos del inventario."); //says data from the inventory
     printf("\nCapacidad\tTiempo de acceso\tCodigo del vendedor\tCosto de fabrica\tPrecio de venta"); this is like the header to make some columns
     printf("\n\nDiscos.cap\tDiscos.miliseg\tDiscos.codven\tDiscos.cost\tDiscos.prec"); //this was meant to be so that it would print out everything that got stored in those variables from the struct
     getch();
     return 0;
    }

    by the way im using borland, i think its a bit different than visual basic right now???

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    #define CAP 10
    #define SEG 10
    #define COD 10
    #define COST 10
    #define PREC 10
    
     struct Discos
     {
      int op;
      int cap[CAP];
      int miliseg[SEG];
      float cost[COST];
      float prec[PREC];
      char codven[COD];
     };
    
    void ingresar(Dicos& dicos, int& index);
    void revisar(Dicos& dicos);
    
    main()
    {
     int op;
    /* int cap[CAP];
     int miliseg[SEG];
     float cost[COST];
     float prec[PREC];
     char codven[COD];*/
    Discos dicos;
    int index;
     while( op != 3 ) // If they type 3 the program ends, I can't read spanish so I don't know what your 3 means
     {
     printf("Programa para capturar datos de la compa&#241;ia de discos");
     printf("\n\nMenu de Opciones: ");
     printf("\n1. Ingresar un nuevo articulo al inventario.");
     printf("\n2. Revisar los datos del inventario.");
     printf("\n3. Salir del programa.");
     printf("\n\nFavor de elegir una opcion: ");
     scanf("&#37;d", &op);
     switch (op)
     {
      case 1:{
    	  ingresar(dicos, index);
    	  break;
              }
      case 2:{
    	  revisar();
    	  break;
              }
      default:{
    	   printf("\n\nOpcion incorrecta.");
    	   break;
    	   }
      }
      }
     return 0;
    }
    
    
    void ingresar(Dicos& dicos, int& index)
    {
    
    
    // int op;
    /*  int cap[CAP];
      int miliseg[SEG];
      float cost[COST];
      float prec[PREC];
      char codven[COD];*/
    
     printf("\nFavor de ingresar los siguientes datos: ");
     printf("\n\n\nCapacidad del disco en byte: ");
     scanf("%d", &dicos[index].cap);
     printf("\nTiempo de acceso en milisegundos: ");
     scanf("%d", &dicos[index].miliseg);
     printf("\nCodigo del vendedor (A, B, C, D): ");
     scanf("%s", &dicos[index].codven);
     printf("\nCosto de fabrica: ");
     scanf("%f", &dicos[index].cost);
     printf("\nPrecio de venta al publico: ");
     scanf("%f", &dicos[index].prec);
    index++;
     return;
    }
    
    void revisar(Dicos& dicos)
    {
     printf("\nDatos del inventario.");
     printf("\nCapacidad\tTiempo de acceso\tCodigo del vendedor\tCosto de fabrica\tPrecio de venta");
     printf("\n\nDiscos.cap\tDiscos.miliseg\tDiscos.codven\tDiscos.cost\tDiscos.prec");
     getch();
     return ;
    }
    You might also want to do something like this. This will ensure that the data is readable from the main function. The variable and index is passed by reference to ingresar.
    index will make sure that you can store more than one of the information, since it will show what index in the array to store the data.
    I also changed it so it creates your structure and save the data into the structure's members.
    Perhaps this might be a little more to what you're looking for? Just be careful - the diacos array has only 10 elements and there's no code to check if you're writing at index 10 or 11 or above, so it can write beyond the end of the array which isn't good.

    Just read your reply, and I think it's a little clearer now. You lack the understanding of functions. You call the function, it returns and continues to execute code after that. The loop will make it jump back again unless you select exit.
    You still lack a good output function. You may want to do like
    Code:
    printf(dicos[some_index_here].cost);
    Or something. You may need to pass dicos as a reference to the function as well.

    I think you're going to have to ask your teacher to properly teach you functions and arguments as well
    Borland and VB are two fundamentally different languages.
    Last edited by Elysia; 10-22-2007 at 01:40 PM.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    yea im still pretty new at this lol sorry

    but yea, im confused now, where did that index come from? lol

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    I added index so that you won't keep overwriting the same data over and over.
    Each time you call the function, it would store the data in dicos[0]. But if I add index and increment the value each time, it will be dicos[0], dicos[1], ...
    That's the secret to adding more data. Unless you just want one? I figured you wanted several since you're using arrays.

  15. #15
    Registered User
    Join Date
    Oct 2007
    Posts
    8
    no i think im starting to get it now thanks, but hey, now i did some of the stuff u told me, and i now get a bigger problem, the program crashes after i try to input some of the data, any help? this is the code i got so far:

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<string.h>
    #define CAP 10
    #define SEG 10
    #define COD 10
    #define COST 10
    #define PREC 10
    int ingresar();
    int revisar();
     struct Discos
     {
      int cap[CAP];
      int miliseg[SEG];
      float cost[COST];
      float prec[PREC];
      char codven[COD];
     } discos;
    main()
    {
     int op=0;
     int index;
    
     while (op != 3)
     {
      printf("Programa para capturar datos de la compaņia de discos");
      printf("\n\nMenu de Opciones: ");
      printf("\n1. Ingresar un nuevo articulo al inventario.");
      printf("\n2. Revisar los datos del inventario.");
      printf("\n3. Salir del programa.");
      printf("\n\nFavor de elegir una opcion: ");
      scanf("%d", &op);
      switch (op)
      {
       case 1:{
      	   ingresar();
          	   return 0;
               }
       case 2:{
    	   revisar();
    	   break;
               }
       default:{
    	    printf("\n\nFin del programa.");
    	    break;
    	    }
      }
     }
     return 0;
    }
    
    
    int ingresar()
    {
     int index;
     int cap[CAP];
     int miliseg[SEG];
     float cost[COST];
     float prec[PREC];
     char codven[COD];
     printf("\nFavor de ingresar los siguientes datos: ");
     printf("\n\n\nCapacidad del disco en byte: ");
     scanf("%d", &cap[index]);
     printf("\nTiempo de acceso en milisegundos: ");
     scanf("%d", &miliseg[index]);
     printf("\nCodigo del vendedor (A, B, C, D): ");
     scanf("%s", &codven[index]);
     printf("\nCosto de fabrica: ");
     scanf("%f", &cost[index]);
     printf("\nPrecio de venta al publico: ");
     scanf("%f", &prec[index]);
     index++;
     return 0;
    }
    
    
    
    int revisar()
    {
     printf("\nDatos del inventario.");
     printf("\nCapacidad\tTiempo de acceso\tCodigo del vendedor\tCosto de fabrica\tPrecio de venta");
     printf("\n\nDiscos.cap[index]\tDiscos.miliseg[index]\tDiscos.codven[index]\tDiscos.cost[index]\tDiscos.prec[index]");
     getch();
     return 0;
    }

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 12:45 PM
  3. Bitmasking Problem
    By mike_g in forum C++ Programming
    Replies: 13
    Last Post: 11-08-2007, 12:24 AM
  4. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 04:42 PM
  5. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21