功能和旋转char

时间:2014-03-11 17:03:19

标签: c++ function rotation

我需要你的帮助!我试图使这个程序能够读取四个字符,然后以旋转模式打印出来。完成旋转四个字符后,它将读取另外四个字符并旋转它们。它重复这个阅读和旋转过程,直到文件结束。

当我运行程序时,它不会打印4次。它打印一次,不旋转任何东西。

这是我的代码

#include <iostream>
using namespace std;

const int NUM_STEPS = 4;   // number of rotation steps to perform

void DisplayRotationPattern(char c1, char c2, char c3, char c4);
void DisplayFourChars(char c1, char c2, char c3, char c4);


//     This function "rotates" four characters c1, c2, c3, and c4.
//     That is, it puts c2 in c1, c3 in c2, c4 in c3, and c1 in c4.
void RotateFourChars(char c1, char c2, char c3, char c4);


int main()
{
   char ch1, ch2, ch3, ch4;   

   cout << "Enter four characters: ";
   cin >> ch1 >> ch2 >> ch3 >> ch4;

   while( cin )
   {
      cout << "The Rotation patterns are:" << endl;

      DisplayFourChars(ch1, ch2, ch3, ch4);



      cout << "Enter four characters: ";
      cin >> ch1 >> ch2 >> ch3 >> ch4;
   }

   return 0;
}


//-----------------------------------------------------------------------
// This function displays the four characters passed to it
// in a rotated pattern.
// params: (in, in, in, in)
//-----------------------------------------------------------------------
void DisplayRotationPattern(char c1, char c2, char c3, char c4)
{
   int count = 1;
   while (count <= NUM_STEPS)
   {

      DisplayFourChars( c1, c2, c3, c4);
      cout << endl;


      RotateFourChars( c1,  c2,  c3, c4 );
      count ++;
   }

}

//-----------------------------------------------------------------------       
// This function "rotates" four characters c1, c2, c3, and c4.
// That is, it puts c2 in c1, c3 in c2, c4 in c3, and c1 in c4.
// params: (inout, inout, inout, inout)
//-----------------------------------------------------------------------
void RotateFourChars(char c1, char c2, char c3, char c4)
{  

      char temp;

       temp = c1;
       c1 = c2;
       c2 = c3;
       c3 = c4;
       c4 = temp;


}

//-----------------------------------------------------------------------
// This function has four input parameters, each of them is a character.
// The function displays the four characters at the beginning of a line.
// params: (in, in, in, in)
//-----------------------------------------------------------------------

void DisplayFourChars(char c1, char c2, char c3, char c4)

{

    cout << c1 << c2 << c3 << c4 << endl;

}

2 个答案:

答案 0 :(得分:3)

问题是你在RoateFourCharacters函数中传递了值。

更改为传递参考:

 void RotateFourChars(char& c1, char& c2, char& c3, char& c4)
 {  
     char temp;

     temp = c1;
     c1 = c2;
     c2 = c3;
     c3 = c4;
     c4 = temp;
 }

或者,您可以使用指针:

 void RotateFourChars(char* c1, char* c2, char* c3, char* c4)
 {  
     char temp;

     temp = *c1;
     *c1 = *c2;
     *c2 = *c3;
     *c3 = *c4;
     *c4 = temp;
 }

 // And when calling, make sure to pass the address of the chars
 RotateFourChars(&c1, &c2, &c3, &c4);

答案 1 :(得分:0)

更改您的RotateFourChars功能,如下所示

在声明部分,

void RotateFourChars(char& c1, char& c2, char& c3, char& c4)

同样在定义部分,

void RotateFourChars(char& c1, char& c2, char& c3, char& c4){
}

还有一件事是你没有在RotateFourChars

中调用main()函数
while( cin )
{
    cout << "The Rotation patterns are:" << endl;
    RotateFourChars(ch1, ch2, ch3, ch4);          // <-- call RotateFourChars here
    //DisplayRotationPattern(ch1, ch2, ch3, ch4); // <-- call DisplayRotationpattern
    DisplayFourChars(ch1, ch2, ch3, ch4);         // <-- instead of calling this