#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
const int index = 5;
int head = 0;
string s[index];
int flag = 1;
int choice;
while (flag)
{
cout << "\n1. Add an Item in
the Chores List.";
cout << "\n2. How many Chores
are in the list.";
cout << "\n3. Show the list
of Chores.";
cout << "\n4. Delete an Item
from the Chores List.";
cout << "\n5. Exit.";
cout << "\n\nEnter your
choice : ";
cin >> choice;
switch (choice)
{
case 1:
{
string str;
if (head <
index)
{
cout << "Enter strings : ";
cin >> str;
s[head] = str;
head++;
}
else
{
index = index * 2;
string temp[index];
for (int i = 0; i<head; i++)
temp[i] = s[i];
//s = temp;
s[head] = str;
head++;
}
break;
}
case 2:
{
cout <<
"\nThe total number of items in the list is : " << head
<< "\n";
break;
}
case 3:
{
for (int i = 0;
i<head; i++)
cout << s[i] << "\n";
break;
}
case 4:
{
head--;
s[head] =
"NULL";
break;
}
case 5:
{
flag = 0;
break;
}
default:
{
cout <<
"\nWrong entry. Please try again.\n";
break;
}
}
}
return 0;
}
Can you help me fix this code? Im getting 2 errors.1 says expression must be a modifiable value. the second says 'index' you cannot assign to a variable this is const. Both are on line 42. Thank you
Write a program for problem 5 in chapter 4.
Use appropriate private data members and provide a public interface for each class that satisfies the description given in the book.
^^^ This is the actual question.
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main()
{
int index = 5;
int head = 0;
string s[index];
int flag = 1;
int choice;
while (flag)
{
cout << "\n1. Add an Item in the Chores List.";
cout << "\n2. How many Chores are in the list.";
cout << "\n3. Show the list of Chores.";
cout << "\n4. Delete an Item from the Chores List.";
cout << "\n5. Exit.";
cout << "\n\nEnter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
{
string str;
if (head < index)
{
cout << "Enter strings : ";
cin >> str;
s[head] = str;
head++;
}
else
{
index = index * 2;
string temp[index];
for (int i = 0; i<head; i++)
temp[i] = s[i];
//s = temp;
s[head] = str;
head++;
}
break;
}
case 2:
{
cout << "\nThe total number of items in the list is : "
<< head << "\n";
break;
}
case 3:
{
for (int i = 0; i<head; i++)
cout << s[i] << "\n";
break;
}
case 4:
{
head--;
s[head] = "NULL";
break;
}
case 5:
{
flag = 0;
break;
}
default:
{
cout << "\nWrong entry. Please try again.\n";
break;
}
}
}
return 0;
}
OUTPUT:
#include <iostream> #include <sstream> #include <string> using namespace std; int main() { const int index...
#include <iostream> #include <vector> #include <iomanip> using namespace std; int main() { const int NUM_ITEMS = 8; vector <double> inverse(NUM_ITEMS); int j; double temp; for (int i = 0; i < NUM_ITEMS; i++) { inverse.at(i) = 1 / (i + 1.0); } cout << fixed << setprecision(2); cout << "Original vector..." << endl; for (int i = 0; i < NUM_ITEMS; i++) { cout << inverse.at(i) << " "; } cout << endl; cout << "Reversed vector..." << endl; for...
Write a psuedocode for this program. #include <iostream> using namespace std; string message; string mappedKey; void messageAndKey(){ string msg; cout << "Enter message: "; getline(cin, msg); cin.ignore(); //message to uppercase for(int i = 0; i < msg.length(); i++){ msg[i] = toupper(msg[i]); } string key; cout << "Enter key: "; getline(cin, key); cin.ignore(); //key to uppercase for(int i = 0; i < key.length(); i++){ key[i] = toupper(key[i]); } //mapping key to message string keyMap = ""; for (int i = 0,j...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...
CODES: main.cpp #include <iostream> #include <string> #include "ShoppingCart.h" using namespace std; char PrintMenu() { char answer; cout << "MENU" << endl; cout << "a - Add item to cart" << endl; cout << "d - Remove item from cart" << endl; cout << "c - Change item quantity" << endl; cout << "i - Output items' descriptions" << endl; cout << "o - Output shopping cart" << endl; cout << "q - Quit" << endl << endl; while (true) {...
#include <iostream> #include <iomanip> #include <cstdlib> #include <fstream> #include <string> #include <vector> #include <sstream> using namespace std; void DisplayMenu() { cout << "1. E games\n"; cout << "2. T games\n"; cout << "3. M games\n"; cout << "4. Total Games\n"; cout << "5. Exit\n"; } double total(double egames, double tgames, double mgames) { int totalgames = egames + tgames + mgames; cout << "There are " << totalgames << " games\n"; return...
#include <iostream> #include <string> using namespace std; int main() { int number; int sum = 0; while(true) { cout << "Please enter a number between 1 and 11: "; cin >> number; if (number >= 1 && number <= 11) { cout << number << endl; sum = sum + number; //only add the sum when number is in range: 1-11, so add wthin this if case } else { cout << number << endl; cout << "Out of range;...
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...