小编gr3*_*bo1的帖子

遍历链表:while(ptr!= NULL)vs while(ptr-> next!= NULL)?

通过定义存储单元

struct node {
    int item;
    node *next;
};
Run Code Online (Sandbox Code Playgroud)

假设ptr指向一个链表,那么put while(ptr!=NULL) while(ptr->next!=NULL)循环通过列表之间是否有区别,直到到达空指针?

c++ linked-list while-loop

4
推荐指数
2
解决办法
1万
查看次数

如何退出嵌套循环

我有类似的东西

   while(playAgain==true)
   {
      cout<<"new game"<<endl; //i know 'using namespace std;' is looked down upon
      while(playerCard!=21)
      {
          *statements*
          if(decision=='n')
          {
              break
          }
       ...
      }
   }
Run Code Online (Sandbox Code Playgroud)

但是当我想要打破两个循环时,那个中断只会突破第一个while循环

c++ loops break while-loop

2
推荐指数
1
解决办法
1万
查看次数

C++ If语句错误:expected';' 在'{'之前

我写这篇文章是为了好玩:

#include <iostream>
#include <cstdlib>
using namespace std;

int Arsenal = rand()%3;
int Norwich = rand()%3;

int main () {
  if (Arsenal > Norwich) {
    cout << "Arsenal win the three points, they are in the Top Four";
    return 0;
  } else if (Arsenal == Norwich) {
    cout << "The game was a draw, both teams gained a point";
    return 0;
  } else (Norwich > Arsenal) {
      cout << "Norwich won, Arsenal lost";
      return 0;
    }
}
Run Code Online (Sandbox Code Playgroud)

我尝试用g ++编译它,但我得到这个错误:

arsenalNorwich.cpp: …
Run Code Online (Sandbox Code Playgroud)

c++ if-statement

1
推荐指数
1
解决办法
9401
查看次数

"打印1到n"功能打印1次两次

 void printInt(int n){
   if(n==1)
     cout<<1<<" ";
   else
     printInt(n-1);
     cout<<n<<" ";
  }
Run Code Online (Sandbox Code Playgroud)

我得到的输出是

 1 1 2 3....n
Run Code Online (Sandbox Code Playgroud)

我在一张纸上写出了函数的实际步骤,但我不知道如何在控制台中打印额外的1(Visual Studio 2010).这是来自过去的硬件解决方案,所以这只是为了理解它是如何工作的.

c++ recursion

1
推荐指数
1
解决办法
142
查看次数

为什么大学计算机科学课程推广'使用命名空间标准'?

到目前为止,我已经在C++上学过两门课,每门课都在不同的学校,并且他们都使用了'using namespace std;' 教基础编程.这可能是巧合,但我不得不自己去找出这样做不好的做法.

c++ std global-namespace

0
推荐指数
1
解决办法
511
查看次数

为什么myString.at(myString.length())不起作用?

我将<string>库包含在我的.cpp文件的顶部,但是当我测试它时

 cout<<myString.at(myString.length());
Run Code Online (Sandbox Code Playgroud)

它应该打印出字符串的最后一个字母,或者至少我认为它应该打印出来.但是我的编译器抛出一个怪异的东西,并向我吐出一堆行话.

我习惯用JavaScript写作,所以我不习惯,好吧....有规则,所以对我来说这很有必要返回字符串的最后一个字符.

c++ string

0
推荐指数
1
解决办法
644
查看次数