将字符串传递给函数以输出它(C++)[关闭]
Posted
技术标签:
【中文标题】将字符串传递给函数以输出它(C++)[关闭]【英文标题】:Passing a string to a function to output it (C++) [closed] 【发布时间】:2016-06-07 00:59:02 【问题描述】:“用 C++ 编写一些东西以在汇编中为微处理器创建方波。 反正我的布局如下:
#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 to file...\n\n"<<prt<<pncnf<<cyc_low;
outfile << "Sqrwv MOVLW 0x02 ; Crude count variable\n"
<< " MOVWF count4 ; Store in COUNT4 to generate wait of 0.5s\n"
<< " BSF STATUS, RP0 ;\n"
<< " BCF STATUS, RP1 ; Select bank 1\n"
<< " MOVLW B'"<<pncnf<<"' ; Write / output only on pin 1\n"
<< " MOVWF TRIS"<<prtlttr<< " ; so portb is 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 to generate\n"
<< " GOTO sqhigh ; a flat high signal of 0.5s\n"
<< " MOVLW 0x02 ; Crude count variable again\n"
<< " MOVWF count5 ; \n"
<< "sqlow NOP ; NOP to ensure correct cycles on high and low\n"
<< " NOP ; mirroring the 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 to generate a flat low signal\n"
<< " GOTO sqlow ; Repeat 4 more times, giving 0.5s\n"
<< " GOTO Sqrwv ; If count4 is now zero, just go back to 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 with Assembly ::\n" // initiate the menu
<< " ::::::::::::::::::::::::::::::::::::::::::\n\n"
<< "Follow the instructions in the list, "
<< "and check for output in sqrwv.txt\n\n"
<< "1. Some important information\n"
<< "2. Enter your clock speed\n" // give the user various options
<< "3. Enter your wave period\n"
<< "4. Or Enter your wave frequency\n"
<< "5. Configure your port and pins\n"
<< "6. Enter your duty cycle\n"
<< "7. Generate your code\n"
<< "8. Exit\n\n" // allow the user to exit using the
<< "Choice: \n";
void menu()
while (1)
system("CLS");
char select[1]; // use as 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 are a few things that you should note: \n"
<< "- Designed for use with P16F877A\n"
<< "- You can enter a delay time of 3.4e +/- 38\n"
<< "- For cycle counts <=1277, you only need to reserve for count1\n"
<< "- For cycle counts <=261125, you need to reserve both count1 and count2\n"
<< "- For cycle counts <=62620865, you need to reserve count1, count2 and count3\n"
<< "- The upper cycle limit for the code created is 62620861 cycles\n"
<< " - This would give a delay of about 62 seconds on a 1MHz clock\n";
system("PAUSE");
system("CLS");
if (select[0] == '2')
system("CLS");
cout << "Your clock speed\n\n";
cout << "Enter your 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 your wave period in decimal form or integer: 3.5\n\n";
cin >> time;
cout << "\n\n";
cycle = time * speed;
cout << "\n\nYou chose a period of " << time << "s\n\n";
cout << "This corresponds to " << cycle << " cycles for one period.\n\n";
system("PAUSE");
system("CLS");
else if (select[0] == '4')
system("CLS");
cout << "Your wave frequency\n\n";
cout << "Enter your wave frequency in decmial form or integer: 0.2\n\n";
cin >> time;
cout << "\n\n";
cycle = speed / time;
cout << "\n\nYou chose a frequency of " << time << "Hz\n\n";
cout << "This corresponds to " << cycle << " cycles for your frequency\n\n";
system("PAUSE");
system("CLS");
else if (select[0] == '5')
system("CLS");
cout << "Your port configuration\n\n";
cout << "Enter one port which you'd like to mess with: e.g. PORTA, PORTB, PORTC\n\n";
cin.ignore();
getline(cin, port);
cout << "\n\n";
cout << "Now enter the write read configuration of the pins on your port: \n e.g. 00000000 = write all, 00001111 = write four, read 4\n\n";
getline(cin, pinconf);
cout << "\n\n";
cout << "Now enter the pin you'd like to 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 be using the TRIS" << port.at(4) << " register and will be toggling pin " << pinhigh<<endl<<endl;
else cout << "\n\nYour port should be five characters long and your pin \nconfiguration should be eight characters because you 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 your duty cycle as a 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 a duty cycle greater than or less than 100% or 0% respectively.\n\n";
system("PAUSE");
system("CLS");
else
cycle_high = cycle * duty;
cycle_low = cycle * (1 - duty);
cout << "\n\nYou chose a duty cycle of " << duty * 100 << "%\n\n";
cout << "This corresponds to " << cycle_high << " cycles for your logic high.\n";
cout << "This corresponds to " << cycle_low << " cycles for your 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 an invalid input, please try again.\n";
cout << "Try to enter a number corresponding to the menu item.\n";
system("PAUSE");
system("CLS");
int main()
menu();
如果我在调用a, ... g
变量的先前if
语句之一中调用该函数,则它“排序”有效。但是,我认为因为我在 while
循环的开头启动了变量,所以它们的范围是整个 while
循环,在 if
语句的内部或外部。
所以,目前我的变量没有被传递给我的wv_calc()
函数,因此,我的代码没有内联变量a, b, ..., g
。
有什么明显的我做错了吗?
【问题讨论】:
您的问题是关于带有while
循环的代码。你为什么要发布这个函数?
尝试使用更有意义的变量名。我猜你重用了变量。
while 循环代码是不言自明的,我认为有人能够发现变量范围/传递变量的错误,但我现在将发布 while
代码。
wv_calc
甚至不使用您传递的大部分变量。
@Barmar,变量名是为了帖子的简单,我的实际程序中有引用周期数、频率、占空比和周期等的变量名。
【参考方案1】:
查看您的代码,您的很多问题都是由于代码组织不当造成的。
例如,假设您设置了select == 2
,您是否看到执行顺序是什么。如下所示,您认为传递给cycle,cycle_high, cycle_low, port and pinconf
的是什么。它们未初始化,因此它们携带的值取决于实现
if (select[0]=='2')
system("CLS");
wv_calc(cycle, cycle_high, cycle_low, port, pinconf, pinhigh, portletter);
system("PAUSE");
system("CLS");
例如,您的程序的用户必须知道在设置 select = '2'
之前始终设置 select == '1'
以初始化 port, pinconf, pinhigh, portletter
。这对您来说可能是显而易见的,但对于以您编写代码的方式生成代码的编译器而言并非如此。
if (select[0]=='1')
system("CLS");
cin.ignore();
getline(cin, port);
getline(cin, port);
getline(cin, pinconf);
getline(cin, pinhigh);
portletter = port.at(4);
我也看不到cycle, cycle_high, cycle_low
的值是在哪里设置的。在致电wv_calc
之前,它们肯定根本没有设置
更新
这是你需要做的:
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 do you need an array here
char select;
menu_bs();
cin >> select;
....
....
【讨论】:
"我了解到根据上面的代码循环,cycle_high 和 cycle_low 没有被初始化,但是它们在菜单的其他地方——在其他 if 语句中。"我应该澄清一下,我没有显示初始化它们的代码......但是,每个人似乎都被我的帖子弄糊涂了,我不妨把完整的代码放上来。 我看到了你的问题。您的变量是 while 循环的本地变量。因此,每次重新执行 while 循环时都会重新创建它们。结果,它们以前的值丢失了。检查我更新的答案。顺便说一句,您不能假设用户不会立即跳到选项 7。如果发生这种情况,那么cycle, cycle_high, cycle_low
将是一堆垃圾
选项 2,3,4,5 在您的情况下并不是真正的选项。它们是选项 6 和 7 工作所必需的。尝试重新组织代码。
嘿,Prab,我想它是这样的,谢谢你发现它。该程序仅供我(可能还有其他一些电子工程师)使用,因此输入验证并不是必需的;我知道这是不好的编程习惯,但是没有人会输入 7 并期望可以编译的代码。再次感谢。以上是关于将字符串传递给函数以输出它(C++)[关闭]的主要内容,如果未能解决你的问题,请参考以下文章
使用 DLL 将 C# StringBuilder / 字符串传递给 C++ char*
如何将字符串传递给 glutCreateWindow() 函数? [关闭]
如何将分隔符数组传递给 Regex.Split 函数? [关闭]