Redo char is undeclared yet it is declared - c++

I'm getting a error with redo char (in bold) on main.cpp when I obviously declare it above. I also would like to know why its asking me to put a semicolon in front of using namespace std since I've never done that before.
//ReverseString.h
#include <iostream>
#include <string>
using namespace std;
class StringClass
{
public:
string string;
int GetStringLength (char*);
void Reverse(char*);
void OutputString(char*);
void UserInputString (char*);
StringClass();
private:
int Length;
}
//StringClass.cpp
#include <iostream>
#include <string>
#include "ReverseString.h"
;using namespace std;
void StringClass::UserInputString(char *string)
{
cout << "Input a string you would like to be reversed.\n";
cin >> string;
cout << "The string you entered: " << string << endl;
}
int StringClass::GetStringLength (char *string)
{
Length = strlen(string);
return Length;
}
void StringClass::Reverse(char *string)
{
int c;
char *front, *rear, temp;
front = string;
rear = string;
GetStringLength(string);
for ( c = 0 ; c < ( Length - 1 ) ; c++ )
rear++;
for ( c = 0 ; c < Length/2 ; c++ )
{
temp = *rear;
*rear = *front;
*front = temp;
front++;
rear--;
}
}
void StringClass::OutputString(char *string)
{
cout << "Your string reversed is: " << string << ".";
}
//Main.cpp
#include <iostream>
#include <string>
#include <fstream>
#include "ReverseString.h"
;using namespace std;
const int MaxSize = 100;
int main()
{
do
{
char string[MaxSize];
**char redo;**
StringClass str;
str.UserInputString(string);
str.Reverse(string);
str.OutputString(string);
//Asks user if they want redo the program
cout << "Would you like to redo the program?\n";
cout << "Please enter Y or N: \n";
**cin >> redo;**
}while(redo == 'Y' || redo == 'y');
}
It's really confusing of why its declaring it but giving an error that it isn't declared.

redo is declared as a local variable within the loop. Its scope starts at the point of declaration, and ends at the closing brace right before while keyword. The name redo is not known within the while condition.

You are missing a semi-colon after your class declaration in ReverseString.h.
The compiler is picking up the error on the line using namespace std; because that is when a problem is first detected. That doesn't mean that you should put the semi-colon there.
Some compilers will hint that you might be missing a semi-colon from a class declaration, while others will not. This mistake is quite common. If you see the missing semi-colon error in a ridiculous place, you should immediately consider that you might have accidentally left one out of your headers.

Related

C++ can't access a public attribute in a class that is returned by the vector's iterator

So I have this class called order, and when a user places an order i invoke that class.
I have another class called orderbook, it is a vector of orders.
I wanted to try to traverse the orderbook upon receipt of a new order to check if there are duplicates, but right now i am stuck traversing the vector.
I am using the iterator and comparing the new order's symbol vs each order in my orderbook's symbol.
I read that the iterator is like a pointer and it will return the memory location of my vector's orders and *it would give me the order itself and if I add a .symbol to the it that should give me the order's symbol attribute, but right now it won't even compile.
I get this error when I try:
../src/oncemore.cpp:123:40: error: 'std::vector<order>::iterator' {aka 'class __gnu_cxx::__normal_iterator<order*, std::vector<order> >'} has no member named 'symbol'
123 | if (_clientOrder.symbol.compare(*it.symbol)==0) {}
| ^~~~~~
below is the code, I have deleted a bunch of stuff not relevant to the question.
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <string.h>
#include "windows.h"
#include <vector>
#include <chrono>
#include <algorithm>
using namespace std;
class order {
public:
string orderID;
char buysell;
string symbol;
double price;
int qty;
void newOrder(string &_orderID, char &_buysell, string &_symbol, double &_price, int &_qty){
orderID=_orderID;
buysell=_buysell;
symbol=_symbol;
price=_price;
qty=_qty;
}
};
class orderbook {
private:
string orderID;
char buysell;
string symbol;
double price;
int qty;
vector<order> the_orderbook;
vector<order>::iterator it;
public:
string insert(order &_clientOrder){
for (it = the_orderbook.begin(); it != the_orderbook.end(); it++) {
if (_clientOrder.symbol.compare(*it.symbol)==0) {} <-- this line fails
}
the_orderbook.push_back(_clientOrder);
return "bla";
}
};
int main() {
cout << "!!!Hello once more" << endl; // prints !!!Hello once more
string user_input = "";
string done= "done trading";
string orderID;
string orderaction;
string orderRecvedTime;
char buysell;
string symbol;
double price;
int qty;
string error;
orderbook thebook;
order user_order;
while(user_input.compare(done) != 0) {
cout << "enter order"<< endl;
getline(cin, user_input);
stringstream lineStream(user_input);
lineStream >>orderaction>>orderID>> buysell >> symbol >> price>> qty;
if (orderaction.compare("D") == 0) {
string tag150;
user_order.newOrder(orderID, buysell,symbol,price,qty);
tag150 = thebook.insert(user_order);
cout << "You made a new order."<< endl;
}
return 0;
}
When you write
*it.symbol
since the operator precedence for . is higher than *, so the expression becomes:
*(it.symbol)
and the error says that an iterator doesn't have a member named symbol which is true.
You can force the precedence by doing:
(*it).symbol
or even better, by using the appropriate operator:
it->symbol

Convert char array to a string with cin.getline(.)

hi guys so my question is how to convert a char array to a string. here is my code:
#include<iostream>
using namespace std;
int main()
{
while (true) {
char lol[128];
cout << "you say >> ";
cin.getline(lol,256);
cout << lol << endl;;
}
return 0;
}
so I want to convert lol to a string variable like "stringedChar" (if thats even english lol)
so I can do stuff like:
string badwords[2] = {"frick","stupid"};
for (int counter = 0; counter < 2;counter++) {
if(strigedChar == badwords[counter]) {
bool isKicked = true;
cout << "Inappropriate message!\n";
}
}
Sorry im just a c++ begginer lol
Do something like this :
as char lol[128];
into string like: std::string str(lol);
Line : cin.getline(lol,256); <--> should be changed to cin.getline(lol,128)
Just invoke std::getline() on a std::string object instead of messing about with a char array, and use std::set<std::string> for badwords as testing set membership is trivial:
#include <iostream>
#include <set>
#include <string>
static std::set<std::string> badwords{
"frick",
"stupid"
};
int main() {
std::string line;
while (std::getline(std::cin, line)) {
if (badwords.count(line) != 0) {
std::cout << "Inappropriate message!\n";
}
}
return 0;
}
Note that this tests whether the entire line is equal to any element of the set, not that the line contains any element of the set, but your code appears to be attempting to do the former anyway.
First off, you have a mistake in your code. You are allocating an array of 128 chars, but you are telling cin.getline() that you allocated 256 chars. So you have a buffer overflow waiting to happen.
That said, std::string has constructors that accept char[] data as input, eg:
#include <iostream>
using namespace std;
int main()
{
while (true) {
char lol[128];
cout << "you say >> ";
cin.getline(lol, 128);
string s(lol, cin.gcount());
cout << s << endl;;
}
return 0;
}
However, you really should use std::getline() instead, which populates a std::string instead of a char[]:
#include <iostream>
#include <string>
using namespace std;
int main()
{
while (true) {
string lol;
cout << "you say >> ";
getline(cin, lol);
cout << lol << endl;;
}
return 0;
}

Passing a string by reference in c++

I am working on a code for my c++ class. The assignment is to read the names from 2 different txt files(already in my directory) and find if the string/name that the user searched for matches any of the names already in the files. My code seems good to me, but I am getting an error in my function prototype saying "string was not declared in this scope." Any solutions? My code is here as follows:
#include <fstream>
#include <string>
#include <vector>
void boysfunc(string&, string&);
void girlsfunc(string&, string&);
using namespace std;
int main()
{
vector<string> boysnames;
vector<string> girlsnames;
string boysname, girlsname;
ofstream outputFile;
cout << "Enter a boy's name, or N if you do not want to
enter a name: ";
cin >> boysname;
cout << "Enter a girl's name, or N if you do not want to
enter a name: ";
cin >> girlsname;
if (boysname != "N")
{
boysfunc(boysname, boysnames);
}
if (girlsname != "N")
{
girlsfunc(girlsname, girlsnames);
}
}
void boysfunc(string &boysname, string &boysnames)
{
outputFile.open("BoysNames.txt");
while(outputFile >> boysnames)
{
/*Declare local variable count to use as a counter*/
int count = 0;
if (boysnames(count) == boysname)
{
outputFile.close();
cout << "The name " << boysname << " is very
popular among boys.";
return;
}
else
{
count++;
}
}
}
void girlsfunc(string &girlsname, string &girlsnames)
{
outputFile.open("GirlsNames.txt");
while(outputFile >> girlsnames)
{
/*Declare local variable count to use as a counter*/
int count = 0;
if(girlsnames(count) == girlsname)
{
outputFile.close();
cout << "The name " << boysname << " is very
popular among girls.";
return;
}
else
{
count++;
}
}
}
There are two major errors that you need to fix here.
using namespace std; must be written before the use of strings if you wish to omit std:: before writing string. Otherwise, you can write std::string& in the function declarations.
boysfunc() and girlsfunc() are taking vector<string>& as the second argument, whereas you incorrectly mentioned string& in the functions' declaration and definition. Fix that.
In this snippet
string s = "hello";
using namespace std;
the type string is not known to the compiler. That's what using namespace std; does. It basically turns string into std::string.
You could swap the 2 lines above, and it will work, but I highly recommend just saying std::string explicitly everywhere. I'm sure your IDE will let you do this easily.

string subscript out of range c++

I need a help in a very basic c++ code.
My program is about guessing name game the problem which i faced is in reading string char by char
#include <iostream>
#include <time.h>
#include <iomanip>
#include <stdlib.h>
#include <fstream>
#include <string>
using namespace std;
void Play(int, int,int, string[], string[]);
string GetRandomName(int, int, int , string[], string[]);
const int ArrayMax = 100;
void Play(int selection, int FArraySize, int MArraySize,string Female[], string Male[])
{
int MAX_TRIES = 3;
int i=0;
ofstream ofFile;
ifstream InFile;
int num_of_wrong_guesses=0;
char letter;
string GuessedName;
GuessedName = GetRandomName(selection, FArraySize, MArraySize, Female, Male);
cout << "Guess the following name:" << endl;
while (GuessedName[i]!= 0 ){
cout<<"?";
i++;
}
cout << "\nEnter a guess letter? or * to enter the entire name" << endl;
cin >> letter;
return;
}
I don't complete coding...
the problem is in the while loop how can i solve it without using cstring?
could you help me?
int i = 0;
while(GuessedName[i] != 0)
{
cout << "?";
i++;
}
Seems like you are trying to print sequence of ? with the length of the string to guess. But you cannot treat std::string as c-string. When its length is n, GuessedName[n] is string subscript out of range - you cannot access one element past end - it's not null-terminated. Use for loop:
for(int i = 0; i < GuessedName.length(); ++i)
cout << "?";
Or simply:
cout << std::string(GuessedName.length(), '?');
Change the while loop like this:
while (GuessedName[i]){
cout<<"?";
i++;
}

Using the getword function c++

Am I using the getword function wrong here? The compiler keeps telling me that there is no member function.
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
using namespace std;
int OccuranceOfString(ofstream & Out)
{
string Occur;
string Temp;
int OccurLength;
int count;
cout << "please enter to string to search for";
cout << endl;
cin >> Occur;
OccurLength = Occur.length();
while(Out.getword(Temp))
{
if (Temp == Occur)
{
count ++;
}
}
return count;
}
Whats wrong with my code? I'm trying to find all occurances of a string with this function
std::ofstream has no getword function: see here.
Perhaps you're thinking of std::getline.
There is no function getword in the header files listed. You simply must construct a function that will extract words from a line. capture a line by
getline(out,line);
line will have your line of string and use line[index] to get continuous characters to be equal to a word.
You can use this
std::string::find
do something like this..
int pos = 0;
int occurrences = 0
string input = "YAaaaAH";
string find = "a";
while(pos != -1){
pos = input.find(find,pos);
occurrences++;
}
text file :
code :
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
ifstream file("DB_name_age.txt");
int index;
string name;
int age;
if(file.is_open())
{
while(file >>index >> name >>age)
{
cout << index <<" "<<name <<" "<<age << endl;
}
}else{
cout<< "file open fail" <<endl;
}
return 0;
}
visual explanation: