Passing a string to a function to output it (C++) -


"writing in c++ create square wave in assembly microprocessor. anyway, layout follows:

#include <iostream> #include <fstream> #include <string>   using namespace std;     void code_calc(float cyc, float cyc_high, float cyc_low, string prt, string pncnf, string pnhgh, char prtlttr) {     ofstream outfile("sqrwv.txt");     if (outfile.is_open()) {         cout << "\n\nwriting file...\n\n"<<prt<<pncnf<<cyc_low;         outfile << "sqrwv   movlw   0x02            ; crude count variable\n"                 <<  "   movwf   count4          ; store in count4 generate wait of 0.5s\n"                 <<  "   bsf status, rp0     ;\n"                 <<  "   bcf status, rp1     ; select bank 1\n"                 <<  "   movlw   b'"<<pncnf<<"'      ; write / output on pin 1\n"                 <<  "   movwf   tris"<<prtlttr<< "          ; portb now(wwww wwww)\n"                 <<  "sqhigh bcf status, rp0     ;\n"                 <<  "   bcf status, rp1     ; select bank 0\n"                 <<  "   bsf "<<prt<<", "<<pnhgh<<"      ; set pin 1 of port b high\n"                 <<  "   call    delay           ; wait 0.1s\n"                 <<  "   decfsz  count4, 1       ; decrement count4 generate\n"                 <<  "   goto    sqhigh          ; flat high signal of 0.5s\n"                 <<  "   movlw   0x02            ; crude count variable again\n"                 <<  "   movwf   count5          ; \n"                 <<  "sqlow  nop             ; nop ensure correct cycles on high , low\n"                 <<  "   nop             ; mirroring bcfs in high\n"                 <<  "   bcf "<<prt<<", "<<pnhgh<<"      ; set pin 1 of port b low\n"                 <<  "   nop             ;\n"                 <<  "   nop             ;\n"                 <<  "   nop             ; 93 cycles low\n"                 <<  "   call    delay           ; wait 0.1s\n"                 <<  "   decfsz  count5, 1       ; decrement count4 generate flat low signal\n"                 <<  "   goto    sqlow           ; repeat 4 more times, giving 0.5s\n"                 <<  "   goto    sqrwv           ; if count4 zero, go high - forever 83 counts\n";         outfile.close();     }      else cout << "couldn't open file. try again."; }  // outputs menu text when called  void menu_bs() {     cout << "       ::::::::::::::::::::::::::::::::::::::::::\n"         << "        :: square wave generator assembly ::\n"        // initiate menu         << "        ::::::::::::::::::::::::::::::::::::::::::\n\n"         << "follow instructions in list, "         << "and check output in sqrwv.txt\n\n"         << "1.  important information\n"         << "2.  enter clock speed\n"           // give user various options         << "3.  enter wave period\n"         << "4.  or enter wave frequency\n"         << "5.  configure port , pins\n"         << "6.  enter duty cycle\n"         << "7.  generate code\n"         << "8.  exit\n\n"           // allow user exit using         << "choice: \n";             }   void menu() {     while (1) {         system("cls");                           char select[1];                         // use menu select variable          menu_bs();         unsigned long long int speed;         float time;         float cycle;         float cycle_high;         float cycle_low;         float period;         float frequency;         float duty;         char portletter;         string port;         string pinconf;         string pinhigh;         cin >> select;         if (select[0] == '1')         {             system("cls");             cin.ignore();             cout << "there few things should note: \n"                 << "- designed use p16f877a\n"                 << "- can enter delay time of 3.4e +/- 38\n"                 << "- cycle counts <=1277, need reserve count1\n"                 << "- cycle counts <=261125, need reserve both count1 , count2\n"                 << "- cycle counts <=62620865, need reserve count1, count2 , count3\n"                 << "- upper cycle limit code created 62620861 cycles\n"                 << "    - give delay of 62 seconds on 1mhz clock\n";             system("pause");             system("cls");         }         if (select[0] == '2')         {             system("cls");             cout << "your clock speed\n\n";             cout << "enter clock speed in integer form: e.g. 5000000\n\n";             cin >> speed;             cout << "\n\nyou chose " << speed << "\n\n";             system("pause");             system("cls");         }          else if (select[0] == '3')         {             system("cls");             cout << "your wave period\n\n";             cout << "enter wave period in decimal form or integer: 3.5\n\n";             cin >> time;             cout << "\n\n";             cycle = time * speed;             cout << "\n\nyou chose period of " << time << "s\n\n";             cout << "this corresponds " << cycle << " cycles 1 period.\n\n";             system("pause");             system("cls");         }         else if (select[0] == '4')         {             system("cls");             cout << "your wave frequency\n\n";             cout << "enter wave frequency in decmial form or integer: 0.2\n\n";             cin >> time;             cout << "\n\n";             cycle = speed / time;             cout << "\n\nyou chose frequency of " << time << "hz\n\n";             cout << "this corresponds " << cycle << " cycles frequency\n\n";             system("pause");             system("cls");         }         else if (select[0] == '5')         {             system("cls");             cout << "your port configuration\n\n";             cout << "enter 1 port you'd mess with: e.g. porta, portb, portc\n\n";             cin.ignore();             getline(cin, port);             cout << "\n\n";             cout << "now enter write read configuration of pins on port: \n e.g. 00000000 = write all, 00001111 = write four, read 4\n\n";             getline(cin, pinconf);             cout << "\n\n";             cout << "now enter pin you'd toggle high/low: e.g. 1-7\n\n";             getline(cin, pinhigh);             pinhigh = pinhigh;             portletter = port.at(4);             if (pinconf.length() == 8 && port.length() == 5) {                 cout << "\n\nyou're configured to: " << port << "(" << pinconf << ")\n\n";                 cout << "you'll using tris" << port.at(4) << " register , toggling pin " << pinhigh<<endl<<endl;             } else  cout << "\n\nyour port should 5 characters long , pin \nconfiguration should 8 characters because have \neight pins. try again.\n\n";             system("pause");             system("cls");         }         else if (select[0] == '6')         {             system("cls");             cout << "your wave duty cycle\n\n";             cout << "enter duty cycle decmial (not percentage): e.g. 50% duty cycle = 0.5\n\n";             cin >> duty;             cout << "\n\n";             if (duty >= 1 || duty <= 0) {                 cout << "\n\nyou can't have duty cycle greater or less 100% or 0% respectively.\n\n";                 system("pause");                 system("cls");             }             else {                 cycle_high = cycle * duty;                 cycle_low = cycle * (1 - duty);                 cout << "\n\nyou chose duty cycle of " << duty * 100 << "%\n\n";                 cout << "this corresponds " << cycle_high << " cycles logic high.\n";                 cout << "this corresponds " << cycle_low << " cycles logic low.\n\n";                 system("pause");                 system("cls");             }         }         else if (select[0] == '7')         {             system("cls");             cout << cycle << cycle_high << port << pinconf;             code_calc(cycle, cycle_high, cycle_low, port, pinconf, pinhigh, portletter);             system("pause");             system("cls");         }         else if (select[0] == '8')                  // quit option         {             system("cls");             exit(0);             system("pause");             system("cls");         }         else          {                                                system("cls");             cout << "you have entered invalid input, please try again.\n";             cout << "try enter number corresponding menu item.\n";             system("pause");             system("cls");         }     } }  int main() {     menu(); } 

if call function inside 1 of previous if statements a, ... g variables called, 'sort-of' works. however, thought because initiated variables @ beginning of while loop, scope whole of while loop, inside or outside of if statements.

so, variables not being passed wv_calc() function, , result, code absent of inline variables a, b, ..., g.

is there obvious i'm doing wrong?

looking @ code, lot of problems due bad organization of code.

for instance lets assume set select == 2 see sequence of execution. follows , think gets passed cycle,cycle_high, cycle_low, port , pinconf. uninitialized values carry implementation dependent

if (select[0]=='2'){             system("cls");             wv_calc(cycle, cycle_high, cycle_low, port, pinconf, pinhigh, portletter);             system("pause");             system("cls"); 

for instance user of program have know set select == '1' initialize port, pinconf, pinhigh, portletter before setting select = '2'. may obvious not compiler generates code way write it.

 if (select[0]=='1'){             system("cls");             cin.ignore();             getline(cin, port);     getline(cin, port);                 getline(cin, pinconf);                 getline(cin, pinhigh);                 portletter = port.at(4); 

also don't see values of cycle, cycle_high, cycle_low being set. not being set @ before call wv_calc

updated

here's need :

void menu() {     float time;     float cycle;     float cycle_high;     float cycle_low;     float period;     float frequency;     float duty;     unsigned long long int speed;     char portletter;     string port;     string pinconf;     string pinhigh;      while (1) {         system("cls");         //char select[1];       //why need array here         char select;         menu_bs();           cin >> select; .... ....