“已启动”变量在此代码中做什么
What does the `started` variable do in this code?
谁能解释一下这段代码是如何工作的?
#include <iostream>
#include <stdio.h>
using namespace std;
int main(int argc, char const *argv[])
{
float longitude;
float latitude;
char info[80];
int started = 0;
cout << "Data = {";
while (scanf("%f, %f, %79[^n]", &latitude, &longitude, info) == 3)
{
if(started)
printf(",n");
else
started = 1;
printf("{latitude: %f, longitude: %f, info: '%s'}",latitude, longitude, info);
}
return 0;
}
我不明白为什么使用 started
变量。为什么选中此变量的值而始终设置为 0?谁能为我逐行解释这段代码?
我不知道为什么在此代码中使用启动变量?
started
变量用于确定这是否是第一次通过循环。 在第二次和以后的刀路中,将打印换行符,因此您不会总是在同一行上打印。
基本上,此代码执行一个循环,重复读取 3 个值(直到用户在不键入 3 个值的情况下按 Enter 键),并将它们打印出来。 第一次,不打印换行符,但每隔一次,将打印出逗号+换行符。
scanf()
3
进入循环返回值,
while(scanf("%f, %f, %79[^]", &latitude, &longitude, info) == 3){
当started
被声明为0
时,if
无法执行else
部分,
if(started) printf(",n");
在else
中,开始变成1
,
else started = 1;
打印三个值,
printf("{latitude: %f, longitude: %f, info: '%s'}",latitude, longitude, info);
在这里started
1
打印n
直到scanf()
不等于3
退出 while 循环,
}
注意"started = 1;"。
在循环的第一次运行时,无需添加逗号分隔符。赋值后,逗号将在每个参数后添加。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 0-1背包代码中的错误.我的代码中有什么错误
- 我是C++编程的新手,这些代码之间有什么区别,我应该使用哪一个
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 这行代码在C++类中意味着什么
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 以下 C++ 代码用于 -> "#define idiv(a, b) (((a) + (b) / 2) / (b))" 是什么?
- 这个带有模板<类 Vector 的C++代码片段有什么问题>
- 此代码中的操作流程是什么?C/C++.
- 当我从下面的代码中删除关键字 virtual 时,它可以正常工作,否则会出现错误。在这里"virtual"字的意义是什么?
- 此代码验证公式是什么意思?
- 需要以下代码的帮助,下面的代码有什么问题
- C++标准提案代码:什么是 N 和 P?
- C - 哈希算法代码什么都没有返回
- 将"12345678"转换为"1-234-5678",我的代码什么也没产生
- 在编译过程中,琐碎的(没有效果的)代码什么时候会被删除
- 有人能告诉我为什么这个代码什么都不打印吗