#include <iostream>
using namespace std;
const int SIZE = 20;
char correctAnswers(char [], int);
char userAnswers(char [], int);
void compareArray(char[] , char [], int [], int, int &);
int main(){
char correct[SIZE];
char userArray[SIZE];
int result[SIZE];
int element;
correctAnswers(correct, SIZE);
cout << "Please enter the student's answers for each of the questions." << endl;
cout << "Press Enter after typing each answer." << endl;
cout << "Please enter only an A, B, C, or D for each question." << endl;
userAnswers(userArray, SIZE);
compareArray(correct, userArray, result, element);
cout << "Array 1 and Array 2 are different at" << element<<" positions: " << " ";
for(int i = 0; i < element; i++)
cout << result[i] << " ";
return 0;
}
char correctAnswers(char correct[], int SIZE){
correct[SIZE] = {'B','D','A','A','A','B','B','A','C','D','B','B','D','A','D','D','A','B','D','A'};
}
char userAnswers(char userArray[], int SIZE){
userArray[SIZE];
for(int i = 0; i < 20; i++){
cout << "Question " << (i+1) << ":";
cin >> userArray[i];
while(userArray[i] != 'A' || userArray[i] != 'B' || userArray[i] != 'C' || userArray[i] != 'D'){
cout << "Invalid input. Choose A,B,C or D." << endl;
cin >> userArray[i];
}
}
}
void compareArray(char correct[], char userArray[], int result[], int &element){
int right = 0, wrong = 0;
element = 0;
for(int i = 0; i < SIZE; i++){
if(userArray[i] == correct[i]){
right = right + 1;
}
else
wrong = wrong + 1;
}
if(right >= 15){
cout << "You Passed!";
}
else
cout << "You Fail.";
cout << "Correct answers = " << right << endl;
cout << "Incorrect answers = " << wrong << endl;
for(int i = 0; i < SIZE; i++){
if(userArray[i] != correct[i]){
result[element] = i+1;
element++;
}
}
}
I'm getting 3 errors and can't figure out why. In my char correctAnswers function, I get an error saying "cannot convert from 'braced-init-list' to 'char" and that the initializer contains too many elements. Also, in my last function it tells me function does not take four elements?
correct in correctAnswer is an array of char, so correct[SIZE] is a particular element of that array (although located past the end of the array), which you are trying to assign an array of characters to.
Even if you removed [SIZE], you'd still have a problem, because you are trying to use an initializer (which, as the same suggests, is for initializing) to make an assignment.
Don't understand what you mean about the last function at all.
Related
For an assignment, we were to fill an array with user-defined characters that stops filling once the user enters a full stop ".". Part of the assignment is to print out the characters entered in the array in reverse, but what I have seems to just print nothing.
First time asking, so apologies if it's a silly question. Thanks in advance.
#include <iostream>
using namespace std;
//Function declarations
bool fillArray(char charArray[], int arraySize, int& numberUsed);
void outputInReverse(const char charArray[], int& numberUsed);
int main() {
const int arraySize = 100;
char charArray[arraySize] = { };
int numberUsed = 0;
//Function calls
cout << "\nFILLING ARRAY....\n";
fillArray(charArray, arraySize, numberUsed);
cout << "\nARRAY OUTPUT....\n";
outputInReverse(charArray, numberUsed);
}
//Function definitions
bool fillArray(char charArray[], int arraySize, int& numberUsed) {
char inputChar;
int index = 0;
const char sentinel = '.';
bool sentinelEntered = false;
bool arrayFull = false;
int count = 0;
//Take user input
for (int i = 0; i < arraySize; i++) {
if ((!sentinelEntered)) {
cout << "Enter up to " << arraySize << " character values. Enter full stop to end. " << "Enter char " << (i + 1) << ": " << endl;
cin >> inputChar;
charArray[index] = inputChar;
//How many entries made
numberUsed = i;
count++;
if ((inputChar == sentinel)) {
sentinelEntered = true;
cout << "Number of entries: " << (count - 1) << endl;
return count;
}
}
}
if (numberUsed == arraySize) {
arrayFull = true;
return arrayFull;
}
return sentinelEntered;
return count;
}
// Reverse
void outputInReverse(const char charArray[], int& numberUsed) {
for (int i = numberUsed; i > 0; i--) {
cout << "Output in reverse: " << charArray[i] << endl;
}
}
FILLING ARRAY....
Enter up to 100 character values. Enter full stop to end. Enter char 1:
a
Enter up to 100 character values. Enter full stop to end. Enter char 2:
b
Enter up to 100 character values. Enter full stop to end. Enter char 3:
c
Enter up to 100 character values. Enter full stop to end. Enter char 4:
d
Enter up to 100 character values. Enter full stop to end. Enter char 5:
e
Enter up to 100 character values. Enter full stop to end. Enter char 6:
.
Number of entries: 5
ARRAY OUTPUT....
Output in reverse:
Output in reverse:
Output in reverse:
Output in reverse:
Output in reverse:
Not sure what you are trying to return from fillArray(), but since its a bool type, assuming you are trying to return if the array is empty or not. Observe added comments to see corrections.
int main() {
const int arraySize = 100;
//corrected
char charArray[arraySize] = { NULL };
int numberUsed = 0;
//Function calls
cout << "\nFILLING ARRAY....\n";
fillArray(charArray, arraySize, numberUsed);
cout << "\nARRAY OUTPUT....\n";
outputInReverse(charArray, numberUsed);
return 0;
}
bool fillArray(char charArray[], int arraySize, int& numberUsed) {
char inputChar;
int index = 0;
const char sentinel = '.';
bool sentinelEntered = false;
bool arrayFull = false;
int count = 0;
//Take user input
for (int i = 0; i < arraySize; i++) {
if ((!sentinelEntered)) {
cout << "Enter up to " << arraySize << " character values. Enter full stop to
end. " << "Enter char " << (i + 1) << ": " << endl;
cin >> inputChar;
//corrected: shifted here so before '.' can enter into array we return
if ((inputChar == sentinel)) {
sentinelEntered = true;
cout << "Number of entries: " << (count) << endl;
//correction: update numberUsed before returning and no of
//elements = count
numberUsed = i;
return count;
}
//correction: array index should not be "index" but i
charArray[i] = inputChar;
//How many entries made
numberUsed = i;
count++;
}
}
if (numberUsed == arraySize)
return true;
return false;
}
void outputInReverse(const char charArray[], int& numberUsed) {
for (int i = numberUsed-1; i >= 0; i--) {
cout << "Output in reverse: " << charArray[i] << endl;
}
}
I am trying to write a small program that takes a user-defined string of a limited selection of characters (lowercase letters, parentheses, and the + and * operators), looks at each of the characters, and organizes them into separate arrays. I thought my approach to this exercise would be fairly straightforward, but I have run into some issues that I cannot figure out.
My problem becomes evident when I attempt to print the individual arrays to the screen. If all of the characters are of the same type (for example, "abcd"), the arrays print as intended. But if there is a combination of character types (for example, "(a+b)"), the arrays print incorrectly. I'm ashamed to say I have been banging my head against this (probably obvious) problem for many hours now and cannot seem to figure out what I have done wrong. Any input would be appreciated - I'm not looking for help writing the program, I just want to know what I have done wrong. I have included my code below:
#include <iostream>
#include <string>
using namespace std;
void charOrganizer(int[], char[], char[], char[], int, int&, int&, int&);
int main()
{
//Variable declaration
string userExpression;
int expressionArray[userExpression.length()];
char letterToken[userExpression.length()];
char parenthesesToken[userExpression.length()];
char plusTimesToken[userExpression.length()];
int letterTokenPos = 0;
int parenthesesTokenPos = 0;
int plusTimesTokenPos = 0;
//Prompt user for string input
cout << "Please enter a mathematical expression only using lowercase letters of the \nalphabet, parentheses, and/or the addition/multiplication operators."<< endl;
cin >> userExpression;
int arraySize = userExpression.length();
for (int i = 0; i < arraySize; ++i)
{
expressionArray[i] = userExpression[i];
}
charOrganizer(expressionArray, letterToken, parenthesesToken, plusTimesToken, arraySize, letterTokenPos, parenthesesTokenPos, plusTimesTokenPos);
//Print tokens to screen
cout << "LowerCase Letter Token values in your string:" << endl;
for (int i = 0; i < letterTokenPos; i++)
{
cout << letterToken[i] << endl;
}
cout << "Parentheses Token values in your string:" << endl;
for (int i = 0; i < parenthesesTokenPos; i++)
{
cout << parenthesesToken[i] << endl;
}
cout << "Operator Token values in your string:" << endl;
for (int i = 0; i < plusTimesTokenPos; i++)
{
cout << plusTimesToken[i] << endl;
}
return 0;
}
void charOrganizer (int charValue[], char letArr[], char parArr[], char pluTimArr[], int size, int& letPosition, int&parPosition, int& operPosition)
{
for (int i = 0; i < size; i++)
{
if (charValue[i] > 96 && charValue[i] < 123)
{
letArr[letPosition] = charValue[i];
cout << "Letter Copy Test: " << letArr[letPosition] << endl;
letPosition++;
}
else if (charValue[i] == 40 || charValue[i] == 41)
{
parArr[parPosition] = charValue[i];
cout << "Parentheses Copy Test: " << parArr[parPosition] << endl;
parPosition++;
}
else if (charValue[i] == 42 || charValue[i] == 43)
{
pluTimArr[operPosition] = charValue[i];
cout << "Operator Copy Test: " << pluTimArr[operPosition] << endl;
operPosition++;
}
else
{
cout << "Invalid input" << endl;
}
}
/*cout << "Print Array Test: " << endl;
for (int i = 0; i < letPosition; i++)
{
cout << letArr[i] << endl;
//cout << parArr[i] << endl;
//cout << pluTimArr[i] << endl;
}*/
}
see your below code. You are using the length of a empty string to declare arrays size like below. In this case all the array size will zero size only. Correct it properly:-
//Variable declaration
string userExpression;
int expressionArray[userExpression.length()];// it would be just like int expressionArray[0];
char letterToken[userExpression.length()];
char parenthesesToken[userExpression.length()];
char plusTimesToken[userExpression.length()];
int expressionArray[30];
char letterToken[30];
char parenthesesToken[30];
char plusTimesToken[30];
Else you declare these variables after you enter your string.
(Sorry if this is formatted terribly. I've never posted before.)
I've been working on a program for class for a few hours and I can't figure out what I need to do to my function to get it to do what I want. The end result should be that addUnique will add unique inputs to a list of its own.
#include <iostream>
using namespace std;
void addUnique(int a[], int u[], int count, int &uCount);
void printInitial(int a[], int count);
void printUnique(int u[], int uCount);
int main() {
//initial input
int a[25];
//unique input
int u[25];
//initial count
int count = 0;
//unique count
int uCount = 0;
//user input
int input;
cout << "Number Reader" << endl;
cout << "Reads back the numbers you enter and tells you the unique entries" << endl;
cout << "Enter 25 positive numbers. Enter '-1' to stop." << endl;
cout << "-------------" << endl;
do {
cout << "Please enter a positive number: ";
cin >> input;
if (input != -1) {
a[count++] = input;
addUnique(a, u, count, uCount);
}
} while (input != -1 && count < 25);
printInitial(a, count);
printUnique(u, uCount);
cout << "You entered " << count << " numbers, " << uCount << " unique." << endl;
cout << "Have a nice day!" << endl;
}
void addUnique(int a[], int u[], int count, int &uCount) {
int index = 0;
for (int i = 0; i < count; i++) {
while (index < count) {
if (u[uCount] != a[i]) {
u[uCount++] = a[i];
}
index++;
}
}
}
void printInitial(int a[], int count) {
int lastNumber = a[count - 1];
cout << "The numbers you entered are: ";
for (int i = 0; i < count - 1; i++) {
cout << a[i] << ", ";
}
cout << lastNumber << "." << endl;
}
void printUnique(int u[], int uCount) {
int lastNumber = u[uCount - 1];
cout << "The unique numbers are: ";
for (int i = 0; i < uCount - 1; i++) {
cout << u[i] << ", ";
}
cout << lastNumber << "." << endl;
}
The problem is my addUnique function. I've written it before as a for loop that looks like this:
for (int i = 0; i < count; i++){
if (u[i] != a[i]{
u[i] = a[i]
uCount++;
}
}
I get why this doesn't work: u is an empty array so comparing a and u at the same spot will always result in the addition of the value at i to u. What I need, is for this function to scan all of a before deciding whether or no it is a unique value that should be added to u.
If someone could point me in the right direction, it would be much appreciated.
Your check for uniqueness is wrong... As is your defintion of addUnique.
void addUnique(int value, int u[], int &uCount)
{
for (int i = 0; i < uCount; i++){
if (u[i] == value)
return; // already there, nothing to do.
}
u[uCount++] = value;
}
This question already has answers here:
How to make cin take only numbers
(2 answers)
Closed 6 years ago.
So the requirements for this program is to be able to increment arrays of the same size (size from 5 to 15 indexes) and increment each element in the array by one using for and while loops. The last task is to take values from the first array and put them in reverse order and assign them to the second array.
So everything works as normal, and the program rejects invalid inputs and does not go into an infinite loop. However, the program accepts some inputs that are not wanted.
For example, I would input something like '12 a' or '7 asdfkla;j lasnfg jasklgn asfg' and it would go through. It is interesting too because the code registers only 12 or 7 and completely ignores the rest. I think it is because once it hits a non-integer character, it would stop ignore the rest.
Why is it ignoring the rest of the input? And is there a way to catch this error from going through?
Also, if you see anything that catches your eye, feel free to critique c: I am always looking to improving.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(time(NULL));
int x;
int j = 0;
bool not_valid = true;
system("color f");
cout << "Program will ask for an input for the size of an array.\n"
<< "With the array size defined, program will generate semi-\n"
<< "true random integers from 0 to 8. First array will then\n"
<< "be assigned to the second in reverse (descending) order.\n\n";
do {
cout << "Enter array size (0 - 15): ";
cin >> x;
if (x >= 5 && x <= 15) {
not_valid = false;
cout << "\nArray size: " << x << endl;
}
else {
cout << "Invalid input.\n\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
} while (not_valid);
int *arr0;
int *arr1;
arr0 = new int[x];
arr1 = new int[x];
for (int i = 0; i < x; i++) {
arr0[i] = rand() % 9;
}
for (int i = 0; i < x; i++) {
arr1[i] = rand() % 9;
}
cout << "\nARRAY 0 (unmodified, for):\n";
for (int i = 0; i < x; i++) {
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 0 (modified, for):\n";
for (int i = 0; i < x; i++) {
arr0[i]++;
cout << arr0[i] << "\t";
}
cout << "\n\nARRAY 1 (unmodified, while):\n";
for (int i = 0; i < x; i++) {
cout << arr1[i] << "\t";
}
cout << "\n\nARRAY 1 (modified, while):\n";
while (j < x) {
arr1[j]++;
cout << arr1[j] << "\t";
j++;
}
int second = x - 1;
for (int i = 0; i < x; i++) {
arr1[second] = arr0[i];
second--;
}
j = 0;
cout << "\n\nARRAY 1 (array 0, descending):\n";
while (j < x) {
cout << arr1[j] << "\t";
j++;
}
cout << endl << endl;
system("pause");
return 0;
}
Take input in string and then check if it's a number or not.
Example:
#include<iostream>
#include<sstream>
#include <string>
using namespace std;
int main()
{
string line;
int n;
bool flag=true;
do
{
cout << "Input: ";
getline(cin, line);
stringstream ss(line);
if (ss >> n)
{
if (ss.eof())
{
flag = false;
}
else
{
cout << "Invalid Input." << endl;
}
}
}while (flag);
cout << "Yo did it !";
}
This is a program that is suppose to compare an answer sheet (which is a .txt file) to user input. Meaning I'm comparing two arrays but I'm having a PITA time making it work. It complies just fine but it just does not compare the user input array to the .txt file array and counts everything as wrong even when I manually put in the correct answers on the user input side. Any advice or suggestions would be appreciated! PS: I cannot use vectors, only arrays. That's not a personal choice but a requirement.
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int CHOICE = 20;
const char KEY = 20;
char correctAnswers[KEY];
char A, B, C, D;
char userAnswers[CHOICE];
char rightOrWrong[CHOICE];
int totalMissed;
int sum = 0;
int count = 0;
ifstream inputFile;
inputFile.open("CorrectAnswers.txt");
while (count < KEY && inputFile >> correctAnswers[count])
count++;
inputFile.close();
for (int qst = 0; qst < 20; qst++)
{
cout << "Please put an answer for the question: " << endl;
cin >> userAnswers[qst];
cout << endl;
if (userAnswers[qst] == correctAnswers[qst])
{
rightOrWrong[qst] = 'C';
}
else
{
rightOrWrong[qst] = 'I';
}
}
for (int qst = 0; qst < 20; qst++)
{
if (rightOrWrong[qst] == 'C')
{
sum += 1;
}
else
{
cout << "Answer #" << qst << " is not correct" << endl;
}
}
totalMissed = 20 - sum;
cout << "This is your final score: " << endl;
cout << "You missed " << (20 - sum) << "/20 of the questions" << endl;
cout << "You overall percentage is " << (sum / 20) << "%." << endl;
if (sum < 14 && sum >= 0)
{
cout << "You have not passed the exam. You must have a 70% or higher to pass. Study harder next time!" << endl;
}
else
{
cout << "Pat yourself on the back! You've passed the test!" << endl;
}
return 0;
}
I apologize if this looks crappily formatted, it doesn't look like this normally.