Very stumped on nested void functions with nexted for / if statements - c++

Ok, I've simplified my problem greatly and it is almost perfect except 1 small nagging problem. The function I couldn't figure out how to work I simply turned into its own cpp file for now.
I cannot for the life of me understand why this thing is running smooth the first time, but every time after that it takes in 2 lines to work..
#include <iostream> // std::cout
#include <iomanip>
#include <cstring>
#include <locale> // std::locale, std::isdigit, std::isalpha
#include <sstream> // std::stringstream
#include <cctype>
#include <string>
#include <limits>
void check_row (int row); // Passes int row (row size) from main
void check_set (int row);
int continue_while_int = 0;
int P_row_value;
int main()
{
int row;
std::cout<<"Enter an int row ";
std::cin >> row;
std::cout << std::endl;
while (continue_while_int == 0)
{
check_set(row);
}
}
void check_set(int row)
{
check_row(row);
}
void check_row (int row) // 1. clear and ignore incase incorrect input is given. 2. Input string. Checks if empty, "exit" or "Exit", valid length, if input
{
int row_copy = row;
std::string row_length_string;
std::string input_length_string;
std::stringstream convert_row; // stringstream used for the conversion
convert_row << row_copy; //add the value of Number to the characters in the stream
row_length_string = convert_row.str(); //set Result to the content of the stream
std::string input_Row;
std::cout << "enter the P_row ";
for (int i = 0; std::cin.clear(),std::cin.ignore(10000,'\n') ; i++) // clears error flags, ignores all input after enter.
{
std::getline(std::cin, input_Row);
// std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::string ifempty = input_Row;
std::string input_Row_copy = input_Row; // creates copy of input
std::stringstream ss_convert_input(input_Row_copy); // gives copy to stringstream instance
int input_length = input_Row_copy.length(); // sets an int as input's length // length of input
int row_length = row_length_string.length(); // sets and int as length of row
int input_as_int; // int to hold input if valid numb string
if(ifempty.length() == 0)
{
std::cout << "error : nothing entered " << std::endl;
}
else if (input_Row_copy == "exit" || input_Row_copy == "Exit")
{
std::cout << "Exiting ";
continue_while_int = 1; // Exit check_set loop
break;
}
else if (input_length != row_length) // if larger than row_length digits or less than or equal to 0 digits
{
std::cout << "error : "<< input_Row_copy;
std::cout << " is outside row input length "<< std::endl << std::endl; // restar
}
else if(!(ss_convert_input >> input_as_int)) // restart if unable to convert
{
std::cout << "error : invalid entry of " << input_Row << std::endl<< std::endl;
}
else if (input_length == row_length)
{
if (input_as_int < row && input_as_int >= 0)
{
P_row_value = input_as_int;
std::cout << "Successfully converted string to int. P_row_value = [" << P_row_value << "]" <<"[Column]";
std::cout.width(20);
std::cout << " && Input length is " << input_length << std::endl << std::endl;
}
else
{
std::cout << "error : " << input_Row_copy << " is not within row boundaries" << std::endl;
}
}
else
{
std::cout << " error: unknown exception : " << input_Row << std::endl << std::endl;
}
convert_row.str(std::string() );
convert_row.clear();
// std::cin.ignore(std::cin.rdbuf()->in_avail(),'\n');
}
}
The output
Enter an int row 4
enter the P_row 3
Successfully converted string to int. P_row_value = [3][Column] && Input length is 1
2
2
Successfully converted string to int. P_row_value = [2][Column] && Input length is 1
3
3
Successfully converted string to int. P_row_value = [3][Column] && Input length is 1
1
1
Successfully converted string to int. P_row_value = [1][Column] && Input length is 1
e
e
error : invalid entry of e
r
r
error : invalid entry of r
error : nothing entered

Related

Test an integer value to determine if it is odd or even in C++

I have to write a program to test an integer value to determine if it is odd or even, and make sure my output is clear and complete. In other words, I have to write the output like "the value 4 is an even integer". I was also hinted that I have to check the value using the remainder modulo.
The issue I have is with the scanf() function. I get a syntax error:
'%=' expected a ')'
How do I fix this?
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
int scanf(%=2 , &number);
if (number == 0)
cout << "the value" << number << "is even";
else
cout << "the value" << number << "is odd";
return 0;
}
You are using scanf() incorrectly (read the scanf() documentation on cppreference.com). The first parameter expects a null-terminated string containing the format to scan, but you are not passing in anything that even resembles a string. What you are passing in is not valid string syntax, per the C++ language standard. That is why you are getting a syntax error.
You need to change this line:
int scanf(%=2 , &number);
To this instead:
scanf("%d", &number);
Though, in C++ you really should be using std::cin instead for input (you are already using std::cout for output):
std::cin >> number;
Try this:
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int main()
{
int number = 0;
cout << "enter an integer ";
if (cin >> number)
{
if ((number % 2) == 0)
cout << "the value " << number << " is even";
else
cout << "the value " << number << " is odd";
}
else
cout << "the value is invalid";
return 0;
}
I know this question is a little dated, however, if you are able to use modern C++ features. You can write a constexpr helper function such as this:
#include <cstdint>
constexpr bool isEven(uint32_t value) {
return ((value%2) == 0);
}
Then in your main function, you can traverse through a loop of N integers and output your display such as:
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isEven(i) ? "even" : "odd") << '\n';
}
return 0;
}
It's literally that simple. Here's another nice feature of using the constexpr helper function... You can also format your output as such:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isEven(i) << '\n';
}
return true;
}
If you are looking for something that is more efficient than using the modulo operator you can bitwise & with the least significant digit... The code above would then become:
#include <cstdint>
constexpr bool isOdd(uint32_t value) {
return (value&1);
}
And using it would be very similar as above, just make sure you reverse the wording in your output to match that from the function being used...
#include <iostream>
#include <iomanip>
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << " is "
<< (isOdd(i) ? "odd" : "even") << '\n';
}
return 0;
}
Again you can use the std::boolalpha manipulator to get this kind of output:
int main() {
for ( int i = 0; i < 100; i++ ) {
std::cout << std::setw(3) << std::setfill('0') << i << ": "
<< std::boolalpha << isOdd(i) << '\n';
}
return true;
}

Eclipse C++ String as function parameter

I am having a problem with an error function which' purpose it is to check a variable and in a certain case tell me the variables name and its value.
I want to give the value as well as the name (as a string) to the function but I am having problems declaring a string somehow (Eclipse MinGW C++).
If anyone could point me to my mistake or show me a workaround that would be great!
This is the code:
#include <string>
#include <iostream>
using namespace std;
int *ierr;
std::string(varname); //problem here, doesnt recognize the string
void error(double varvalue, std::string varname)
{
if (varvalue == 0 ) {
*ierr = 11;
cout << "Error: " << varname << " has an invalid value (equal 0)";
cout << "Error number " << *ierr << endl;
return;
}
if (varvalue < 0 ) {
*ierr = 10;
cout << "Error: " << varname << " has an invalid value (" << varvalue << " , smaller 0)";
cout << "Error number: " << *ierr << endl;
return;
}
}
int main() {
int Par = 0;
error(Par,"Par"); //test variable
}
You did not correctly define a string value. The syntax is incorrect. On a more serious note, you declare an uninitialized pointer *ierr and it's causing seg faults at run-time.
I am having a problem with an error function which' purpose it is to check a variable and in a certain case tell me the variables name and its value.
The code below will do that for you but it's important to understand when to use pointers.
Furthermore you should probably read up a little on strings and get familiar with what a string definition, copy, initialization, etc, look like.
#include <string>
#include <iostream>
using namespace std;
void error(double varvalue, std::string varname)
{
if (varvalue == 0 ) { //if varvalue is equal to 0
cout << "Error: " << varname << " has an invalid value (equal 0)";
cout << "Error number " << varvalue << endl;
return;
}
if (varvalue < 0 ) { //if varvalue is less than 0
cout << "Error: " << varname << " has an invalid value (" << varvalue << " , smaller 0)";
cout << "Error number: " << varvalue << endl;
return;
}
if (varvalue > 0) { //if varvlue is greater than 0
cout<<varname<<" has value: "<<varvalue<<endl;
}
}
int main() {
int Par = 10; // Set initial Par value to 10
error(Par,"Par"); //test variable
}

creating a c++ program that displays hexadecimal-formatted data from a bmp file

I'm trying to create a program that displays output of a bmp file in the form of hexadecimal. So far I get the output, but I need it to be organized a certain way.
The way it needs to be organized is with the address of the bmp file to be on the left column and then 16 bytes of data in hex across each row in the order they appear in the file. While leaving an extra space between every 8 bytes. So far, I got the hexadecimal to show up, I just need help with organizing it.
What I have:
What I'm trying to make it look like:
Here is my code:
#include <iostream> // cout
#include <fstream> // ifstream
#include <iomanip> // setfill, setw
#include <stdlib.h>
using namespace std; // Use this to avoid repeated "std::cout", etc.
int main(int argc, char *argv[]) // argv[1] is the first command-line argument
[enter image description here][1]{
// Open the provided file for reading of binary data
ifstream is("C:\\Users\\Test\\Documents\\SmallTest.bmp", ifstream::binary);
if (is) // if file was opened correctly . . .
{
is.seekg(0, is.end); // Move to the end of the file
int length = is.tellg(); // Find the current position, which is file length
is.seekg(0, is.beg); // Move to the beginning of the file
char * buffer = new char[length]; // Explicit allocation of memory.
cout << "Reading " << length << " characters... ";
is.read(buffer, length); // read data as a block or group (not individually)
if (is)
cout << "all characters read successfully.\n";
else
cout << "error: only " << is.gcount() << " could be read.\n";
is.close();
// Now buffer contains the entire file. The buffer can be printed as if it
// is a _string_, but by definition that kind of print will stop at the first
// occurrence of a zero character, which is the string-ending mark.
cout << "buffer is:\n" << buffer << "\n"; // Print buffer
for (int i = 0; i < 100; i++) // upper range limit is typically length
{
cout << setfill('0') << setw(4) << hex << i << " ";
cout << setfill('0') << setw(2) << hex << (0xff & (int)buffer[i]) << " ";
}
delete[] buffer; // Explicit freeing or de-allocation of memory.
}
else // There was some error opening file. Show message.
{
cout << "\n\n\tUnable to open file " << argv[1] << "\n";
}
return 0;
}
You could do it something like this:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <cctype>
std::ostream& fullLine(std::ostream& out, const std::vector<uint8_t>& v, size_t offset)
{
//save stream state so we can restore it after all the hex/setw/setfill nonsense.
std::ios oldState(0);
oldState.copyfmt(out);
out << std::hex << std::setfill('0') << std::setw(8) << offset << " ";
for (size_t i = 0; i < 16; ++i)
{
if (i == 8) out << " ";
out << std::hex << std::setfill('0') << std::setw(2) << static_cast<uint32_t>(v[i + offset]) << " ";
}
out << " ";
//restore stream state to print normal text
out.copyfmt(oldState);
for (size_t i = 0; i < 16; ++i)
{
out << (std::isprint(v[i + offset]) ? static_cast<char>(v[i + offset]) : '.');
}
out << "\n";
return out;
}
int main()
{
std::vector<uint8_t> data;
std::ifstream f("test.txt", std::ios::binary);
if (f)
{
f.seekg(0, f.end);
data.resize(static_cast<size_t>(f.tellg()));
f.seekg(0, f.beg);
f.read((char*)data.data(), data.size());
const size_t numFullLines = data.size() / 16;
const size_t lastLineLength = data.size() % 16;
for (size_t i = 0; i < numFullLines; ++i)
{
if (!fullLine(std::cout, data, i * 16))
{
std::cerr << "Error during output!\n";
return -1;
}
}
}
return 0;
}
There's probably a fancy way to do it, but I usually go for brute force when I'm looking for particular output using iostreams.
How to handle the partial last line is up to you. :)
Use the % operator to break the line after every 16th count:
cout << hex;
for(int i = 0; i < 100; i++)
{
if(i && (i % 16) == 0)
cout << "\n";
cout << setfill('0') << setw(2) << (buffer[i] & 0xFF) << " ";
}
I need it to be organized a certain way.
In another answer, I submitted this form of dumpByteHex()... perhaps it can help you achieve what you want. (see also https://stackoverflow.com/a/46083427/2785528)
// C++ support function
std::string dumpByteHex (char* startAddr, // reinterpret_cast explicitly
size_t len, // allows to char* from T*
std::string label = "",
int indent = 0)
{
std::stringstream ss;
if(len == 0) {
std::cerr << "\n dumpByteHex() err: data length is 0? " << std::endl << std::dec;
assert(len != 0);
}
// Output description
ss << label << std::flush;
unsigned char* kar = reinterpret_cast<unsigned char*>(startAddr); // signed to unsigned
std::string echo; // holds input chars until eoln
size_t indx;
size_t wSpaceAdded = false;
for (indx = 0; indx < len; indx++)
{
if((indx % 16) == 0)
{
if(indx != 0) // echo is empty the first time through for loop
{
ss << " " << echo << std::endl;
echo.erase();
}
// fields are typically < 8 bytes, so skip when small
if(len > 7) {
if (indent) { ss << std::setw(indent) << " "; }
ss << std::setfill('0') << std::setw(4) << std::hex
<< indx << " " << std::flush;
} // normally show index
}
// hex code
ss << " " << std::setfill('0') << std::setw(2) << std::hex
<< static_cast<int>(kar[indx]) << std::flush;
if((indx % 16) == 7) { ss << " "; wSpaceAdded = true; } // white space for readability
// defer the echo-of-input, capture to echo
if (std::isprint(kar[indx])) { echo += kar[indx]; }
else { echo += '.'; }
}
// finish last line when < 17 characters
if (((indx % 16) != 0) && wSpaceAdded) { ss << " "; indx++; } // when white space added
while ((indx % 16) != 0) { ss << " "; indx++; } // finish line
// the last echo
ss << " " << echo << '\n';
return ss.str();
} // void dumpByteHex()
Output format:
0000 11 22 33 44 55 66 00 00 00 00 77 88 99 aa ."3DUf....w...

how can I find the sequence number (index) of word in such a paragraph c++?

I'm working on a project which needs to find the number of words and the indices of each word in the paragraph ...I have written the code which is counting the number of word in a string but I stuck with finding the indices of words,
such as : Hi John How are you I miss you ..
I need to print the indices like : 0 1 2 3 4 5 6 7
here is the code:
int _tmain(int argc, _TCHAR* argv[])
{
int count_words(std::string);
std::string input_text;
std::cout<< "Enter a text: ";
std::getline(std::cin,input_text);
int number_of_words=1;
int counter []={0};
for(int i = 0; i < input_text.length();i++)
if(input_text[i] == ' ')
number_of_words++;
std::cout << "Number of words: " << number_of_words << std::endl;
//std:: cout << number_of_words << std::endl;
system ("PAUSE");
}
Hopefully this helps. Edited to include use of count_words function.
#include <iostream>
#include <sstream>
void count_words(std::string);
int main(){
std::string input_text, output_text;
std::cout<< "Enter a text: ";
std::getline(std::cin,input_text);
count_words(input_text);
system ("PAUSE");
return 0; //MUST RETURN AN INTEGER VALUE FROM 'INT MAIN'
}
void count_words(std::string inputString){
std::string output_text;
std::stringstream indexes;
int number_of_words=0; //If there are no words, it would be false, make it 0.
//int counter []={0}; //This serves no purpose.
if(!inputString.empty()){// test to make sure it isn't empty.
number_of_words++;
for(int i = 0; i < inputString.length();i++){ // For loops should have curly braces {} containing their statement.
if(inputString[i] == ' '){
number_of_words++;
}
if((isalpha(inputString[i]))&&inputString[i-1]==' '){ //test for following space separated word
indexes << i << " ";
}
}
}
output_text = indexes.str(); //convert stringstream to string
std::cout << "Number of words: " << number_of_words << std::endl;
//std:: cout << number_of_words << std::endl; //duplicate info
std::cout << "Indexes: " << output_text << std::endl;
}
I'm not sure if i understand the question. You only need print the "indices"?? like this? (Using your own code)
#include <iostream>
#include <vector>
#include <string>
void stringTokenizer(const std::string& str, const std::string& delimiter, std::vector<std::string>& tokens) {
size_t prev = 0, next = 0, len;
while ((next = str.find(delimiter, prev)) != std::string::npos) {
len = next - prev;
if (len > 0) {
tokens.push_back(str.substr(prev, len));
}
prev = next + delimiter.size();
}
if (prev < str.size()) {
tokens.push_back(str.substr(prev));
}
}
int main()
{
std::vector <std::string> split;
std::string input_text;
std::cout<< "Enter a text: ";
std::getline(std::cin,input_text);
stringTokenizer(input_text, " ", split);
int number_of_words = 0;
for (std::vector<std::string>::iterator it = split.begin(); it != split.end(); it++, number_of_words++) {
std::cout << *it << " " << number_of_words << std::endl;
}
}

Program soft-locking when calling second* function

So, I'm practising my C++ code by writing a program that creates a matching-pairs game out of a grid with user-specified size. To do this I've separated each action (initialising the grid, drawing the grid, hiding the pairs and finding the pairs from user input) into a function.
Unfortunately, at some point - I can't tell when or how - the program began failing to call the second* and subsequent functions. After calling drawGrid, it should move on to hidePairs immediately - however, it just stops at that step. The cursor in the command window keeps blinking, so the program's running just fine, but nothing I can do will make it progress.
I've checked via debugging, and it does successfully execute drawGridand moves on to the next line in main, but then no more code gets executed. What am I doing wrong?
*EDIT: third function, it successfully draws the grid, THEN stops before hiding the pairs. Unnecessary calls-by-reference removed and all functions added for clarity. My bad :P
#include <iostream>
#include <random>
#include <algorithm>
void drawGrid(int gridX, int gridY, char gridArray[][30]);
void hidePairs(int gridX, int gridY, char pairsArray[][30], int *numPairs);
void findPairs(int gridX, int gridY, char gridArray[][30], char pairsArray[][30], int *numPairs);
void initialiseGrid(int gridX, int gridY, char gridArray[][30], char pairsArray[][30]);
int main()
{
int gridX, gridY, numPairs;
//Ask user for gridX(width) and gridY(height)
std::cout << "Please enter the width and height of the grid you want to use." << std::endl;
bool gridIsNotEven = true;
while (gridIsNotEven == true)
{
std::cin >> gridX >> gridY;
if ((gridX*gridY) % 2 == 0)
{
gridIsNotEven = false;
}
else
{
std::cout << std::endl << "The grid produced by these two numbers has an odd number of spaces." << std::endl;
}
}
if (gridX*gridY > 30)
{
std::cout << "This grid is larger than recommended." << std::endl;
}
gridX++;
gridY++;
char gridArray[30][30];
char pairsArray[30][30];
numPairs = ((gridX*gridY) / 2);
//Func : initialiseGrid
initialiseGrid(gridX, gridY, gridArray, pairsArray);
//Func : drawGrid
drawGrid(gridX, gridY, gridArray);
//Func : hidePairs
hidePairs(gridX, gridY, pairsArray, &numPairs);
//Func : findTreasure
findPairs(gridX, gridY, gridArray, pairsArray, &numPairs);
system("Pause");
return 0;
}
void drawGrid(int gridX, int gridY, char gridArray[][30])
{
int printX, printY;
//For(printX = 0, printX < gridX, printX++)
for (printY = 0; printY < gridY; printY++)
{
//For(printY = 0, printY < gridY, printY++)
for (printX = 0; printX < gridX; printX++)
{
std::cout << gridArray[printX][printY] << " ";
}
//END FOR
//Print new line
std::cout << std::endl;
}
//END FOR
}
void hidePairs(int gridX, int gridY, char pairsArray[][30], int *numPairs)
{
int pairsMade, halfPair, curPairX = 0, curPairY = 0;
char pairSymbol = '!';
for (pairsMade = 0; pairsMade < *numPairs; pairsMade++)
{
halfPair = 0;
while (halfPair < 2)
{
curPairX = rand() % gridX;
curPairY = rand() % gridY;
if (pairsArray[curPairX][curPairY] == '?')
{
pairsArray[curPairX][curPairY] = pairSymbol;
halfPair++;
}
}
pairSymbol++;
}
}
void findPairs(int gridX, int gridY, char gridArray[][30], char pairsArray[][30], int *numPairs)
{
int guess1X = 0, guess1Y = 0, guess2X, guess2Y, printChar, pairsFound = 0;
//Display pairs
char pairSymbol = '!';
printChar = 0;
std::cout << std::endl << "The following symbols will be used in the grid:" << std::endl << std::endl;
while (printChar < *numPairs)
{
std::cout << pairSymbol << std::endl;
pairSymbol++;
printChar++;
}
//while ((guessX != treasureX) OR(guessY != treasureY))
while (pairsFound != *numPairs)
{
// User input : guessX and guessY
std::cout << std::endl << "Please enter the co-ordinates of your first guess (e.g. 'X Y')" << std::endl << std::endl;
std::cin.clear();
std::cin >> guess1X >> guess1Y;
gridArray[guess1X][guess1Y] = pairsArray[guess1X][guess1Y];
drawGrid(gridX, gridY, gridArray);
std::cout << std::endl << "Please enter the co-ordinates of your second guess (e.g. 'X Y')" << std::endl << std::endl;
std::cin.clear();
std::cin >> guess2X >> guess2Y;
gridArray[guess2X][guess2Y] = pairsArray[guess2X][guess2Y];
drawGrid(gridX, gridY, gridArray);
if (guess1X > gridX || guess1X < 1 || guess1Y > gridY || guess1Y < 1)
{
std::cout << std::endl << guess1X << ", " << guess1Y << " does not lie inside the grid. Try again." << std::endl;
continue;
}
else if (gridArray[guess1X][guess1Y] == gridArray[guess2X][guess2Y])
{
pairsFound++;
}
else
{
std::cout << std::endl << "Pair not matching" << std::endl << std::endl;
gridArray[guess1X][guess1Y] = '?';
gridArray[guess2X][guess2Y] = '?';
}
// END IF
}
//Print ‘Success! etc.’
std::cout << std::endl << "Success! You found all the pairs!" << std::endl << std::endl;
}
void initialiseGrid(int gridX, int gridY, char gridArray[][30], char pairsArray[][30])
{
int printX, printY;
for (printY = 0; printY < gridY; printY++)
{
for (printX = 0; printX < gridX; printX++)
{
if ((printX == 0))
{
gridArray[0][printY] = printY + '0';
pairsArray[0][printY] = printY + '0';
}
else if ((printY == 0))
{
gridArray[printX][0] = printX + '0';
pairsArray[printX][0] = printX + '0';
}
else
{
gridArray[printX][printY] = '?';
pairsArray[printX][printY] = '?';
}
}
}
}
You initialised the number of pairs to numPairs = gridX * gridY / 2, but that size (gridX, gridY) contains also the header column and row, that do not play a role in the pairing.
So to fix this, your numPairs should be (gridX-1) * (gridY-1) / 2.
Else, the pairs will have been exhausted, but the pairing function is still looking! This is your infinite loop.
I also advise you to find a better name for gridX and gridY to indicate that the headers are counted, or to be very careful in the rest of your code that you understand what gridX and gridY mean and that the grid only starts being an actual thing at index 1 rather than index 0.
It seams it fails only if you add numbers 30 and higher caused by array size
I dont know your settings but ensure you have compiler setting of language standard like GNU C++11
it should work
Turns out there's an infinite loop in the hidePairs function. It's not reading the correct co-ordinates in the pairsArray, so there are spaces it never fills in. And since the loop is only set to end when all unused spaces are filled, it never stops.
Advice for others: always sanity-check your loops!