小编TRE*_*MOR的帖子

不同的时候循环是或否条件

这是令人沮丧的,看起来我希望循环只有在用户输入"N"或"n"时才会中断.

#include <iostream>

int main()
{
char abc;
std::cin >> abc;

while (abc != 'N' || abc != 'n')
{
      std::cout << "hello world\n";
      std::cin >> abc;
}
system("pause");   
return 0;
}
Run Code Online (Sandbox Code Playgroud)

这些工作:

while(abc == 'Y' || abc == 'y')
while(abc == 'N')
Run Code Online (Sandbox Code Playgroud)

但为什么?

c++ while-loop

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

C - 输入2个值,将2个值返回MAIN.交换

我需要制作一个程序来按顺序排列4个数字,比如9 2 8 3到2 3 8 9.所以我想到了使用swap的逻辑.我将使用if/else或switch语句,但首先我需要解决此问题.我想输入2个整数(稍后进行比较),然后交换它们并将其返回到main函数.我该怎么做呢.请提供最好的解决方案,虽然我没有教过指针,结构等.

#include <stdio.h>

int swap(int x, int y)
{   
int c;
    c = x;
    x = y;
    y = c;
    return x, y;
}

int main()
{
int a = 5, b = 7;

    printf("a=%d b=%d\n", a, b);
    swap(a, b);
    printf("a=%d b=%d\n", x, y);
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我希望最终输出为7,5.谢谢!

c swap return function

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

2个简单的循环不能协同工作,分段错误

好吧,所以要么我使用FOR或WHILE循环,它们可以单独工作,但是当我在它之后键入相同类型的循环时,它会编译但是在运行之后它会给出错误"seg fault(core dumped)"或者有时候"Bus"错误核心倾销"

我现在正在使用2个2D阵列,我需要制作通用矩阵程序.现在代码是输入.如果我在/*和*/之间添加一个循环,剩下的循环工作正常!

#include <stdio.h>

int main()
{
int i, j, k, l; //I, J for array matrix a, K, L for matrix b
int a[i][j]; //matrix of size i by j
int b[k][l];
int rowa=0, cola=0, rowb=0, colb=0;
//rowa is ROW no. of array a and colb e.g Column no. for array b

printf("size of matrix a: ");
scanf("%d\n%d", &i, &j); //i is row, j is column

printf("size of matrix b: ");
scanf("%d\n%d", &k, &l); //k is row, …
Run Code Online (Sandbox Code Playgroud)

c loops segmentation-fault multidimensional-array

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

如何打印此手动制作的字符串

我只需要反转一个字符串,所以我声明了一个新的字符串变量并迭代地复制了这些元素.现在我想打印反转的字符串,cout << reversed;但这不打印任何东西.我可以通过for循环打印reverse[i]直到尺寸但是有更好的方法吗?

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

int main()
{
    string original = "hello";
    string reverse;
    int i, j = 0, size = original.length();

    for (i = size - 1; i >= 0; i--) // original's last as reversed's first
    {
        reverse[j] = original[i]; 
        j++;
    }
    reverse[j] = '\0'; //last value as null
    cout << "original string = " << original << endl;
    cout << "reversed string = " << reverse  << endl; …
Run Code Online (Sandbox Code Playgroud)

c++ string

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

如何在另一个类中初始化我的对象数组

我正在制作一个游戏,其中我控制游戏对象的主要类是"inGame".将在"inGame"中组成其他几个自定义类.

喜欢,

class mouse
{
  int x, y;
  bool alive;
  float speed;
  public:
    mouse(int xx, int yy, float spd, bool alv)
    {
        x = xx; y = yy; speed = spd; alive = alv;
    }
    // member functions
};

class inGame
{
  mouse m1; 
  dog d1; // single object which are easy to construct
  public:
    inGame() : d1(200, 400, 1.5, false), m1(40, 40, 0.5, false) //This works fine
    {} //coordinates fixed and set by me
    // member functions
};
Run Code Online (Sandbox Code Playgroud)

但现在可以说我想要3个鼠标.所以我想到了m1 …

c++ class initialization-list

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

添加时Struct的地址不一致

我正在处理一个内存,我在分配的块中使用此标头.我正在尝试使用指针算法来返回新区域.这是一个简单的问题.当我将1,2,3添加到整数地址时,编译器会带来下一个第一个,第二个,第三个整数的地址,它们都会因sizeof(int)而异.但是在这段代码中,一个结构的大小为8.而地址的变化是2和8.下面是代码,输出得到:

#include <stdio.h>
#include <stdlib.h>

typedef struct header {
int size;
int magic;
}header;

int main() {

int n = 10;
printf("size of int %d\n", sizeof(n));
printf("addr %p\n", &n);
printf("addr+1 %p\n", &n+1);
printf("addr+2 %p\n", &n+2);
printf("addr+3 %p\n", &n+3);

header *h = malloc(sizeof(header));
printf("size of header %d\n", sizeof(header));
printf("addr %p\n", h);
printf("addr+1 %p\n", h+1);
printf("addr+2 %p\n", h+2);
printf("addr+3 %p\n", h+3);


return 0;
}
Run Code Online (Sandbox Code Playgroud)

OUTPUT

size of int 4
addr 0xbfeb7898
addr+1 0xbfeb789c
addr+2 0xbfeb78a0
addr+3 0xbfeb78a4

size of header 8
addr …
Run Code Online (Sandbox Code Playgroud)

c ubuntu struct pointers

-3
推荐指数
1
解决办法
43
查看次数