Assigning one array element to another array.
Hi, I was doing my assignment which states:
The user to enter a string which should be stored in Input_String[].
2) Then reverse the string and save it to another array called Output_String[].
I am having trouble with the second part where I need to save the string to another array. I am not sure how would I do. Here is what I have done:
// Hw-4_Bhasin.cpp : Defines the entry point for the console application.
//void rev_str();
#include "stdafx.h"
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
char option;
void rev_str();
double Mean(const int Data[5][4], int, int);
void frequency(const int Data[5][4], int, int);
cout<<"\n Please choose from the given menu.";
cout<<"\n R{Reverse String] M[Matrix] Q[Quit]."<<endl;
cin>> option;
cin.ignore();
switch(option)
{
case 'R':
case 'r':
rev_str();
break;
}
system("pause");
return 0;
}
void rev_str()
{
const int MAX = 20;
char Input_String[MAX];
char Output_String[MAX];
cout<<"Please enter a string."<<endl;
cin.get(Input_String, MAX);
for(int i=MAX; i>=0; i--)
cout<<Input_String[i]<<endl;
Output_String = Input_String; // ERROR: LEFT OPERAND MUST BE 1-VALUE.
system("pause");
return;
}
Thanks.
robgeek
Junior Poster in Training
50 posts since May 2008
Reputation Points: 10
Solved Threads: 0
you can not copy one character array to another using the = operator -- that only works with std::string.
The assignment wants you to copy the string in reverse order. The only way to do that is one character at a time, something like you did in the previous line when you displayed it in reverse order. When doing it one character at a time you can use the = assignment operator. Something like this, where s2 is the original string and s1 is the new string.
for(int i = 0; j = MAX-1; i < MAX; i++, j--)
s1[j] = s2[i];
You can also do it with pointers
char* p1 = s1; // new string
char* p2 = s2 + strlen(s2); // original string
while( p2 > s2 )
*p1++ = *p2--;
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Thanks. I made changes to my co[codde as you said but I am getting syntax errors which I am not able to solve lf anyone can take a look that would be really great.
// Hw-4_Bhasin.cpp : Defines the entry point for the console application.
//void rev_str();
#include "stdafx.h"
#include<iostream>
#include<string>
#include<cmath>
using namespace std;
int main()
{
char option;
char sub_menu;
int Data[5][4] = {0, 0, 0, 0};
void rev_str();
double Mean(const int Data[5][4], int, int);
void frequency(const int Data[5][4], int, int);
cout<<"\n Please choose from the given menu.";
cout<<"\n R{Reverse String] M[Matrix] Q[Quit]."<<endl;
cin>> option;
cin.ignore();
switch(option)
{
case 'R':
case 'r':
rev_str();
break;
case 'M':
case 'm':
cout<<"Please choose from the following sub menu."<<endl;
cout<<" A{Average] F[Frequency] M[Median] P[Previous Menu]"<<endl;
cin>> sub_menu;
cout<<"Please enter 5 integer sets of 4 numbers."<<endl;
cin>> Data[5][4];
switch(sub_menu)
{
case 'A':
case 'a':
break;
}
}
system("pause");
return 0;
}
void rev_str()
{
const int MAX = 20;
char Input_String[MAX];
char Output_String[MAX];
cout<<"Please enter a string."<<endl;
cin.get(Input_String, MAX);
for(int i=0; int j= MAX-1;i<MAX; i++, j--)
cout<<Input_String<<endl;
Output_String[j] = Input_String[i];
system("pause");
return;
}
All errors point to for loop.
Errors:
Error 1 error C2146: syntax error : missing ')' before identifier 'i'
Error 3 error C2059: syntax error : ';'
Error 4 error C2059: syntax error : ')'
Error 5 error C2146: syntax error : missing ';' before identifier 'cout'
Error 6 error C2065: 'j' : undeclared identifier
Error 7 error C2065: 'i' : undeclared identifier
Thanks.
robgeek
Junior Poster in Training
50 posts since May 2008
Reputation Points: 10
Solved Threads: 0
>>for(int i=0; int j= MAX-1;iint i = 0 , not a semicolon and don't use int j
Two more errors:
1) you need to use { and } for multi-line statements
2) You have i and j in reverse order.
for(int i=0, j= MAX-1;i<MAX; i++, j--)
{
cout<<Input_String<<endl;
Output_String[i] = Input_String[j];
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343