Can somebody help me with these?

0 Ed Gon · November 27, 2014
The following are the commands:
ADD x,y; where x and y are literal integer values and ADD is all capital letters.  The ADD command do not display the result but merely performs addition on whatever values are there for x and y. The result of the operation is saved on a special variable called AX.

PRT AX - display the value of AX.

EXIT – Exits the program.

Your program should continue asking valid commands from the user until the user typed EXIT.

Example of valid inputs from the user:
ADD 3,5
ADD    4,   3 
PRT AX
EXIT

Example of invalid inputs from the user:
·          add 3,4
·          ADD x,4
·          ADD x,y
·          DISPLAY AX
·          PRT ax
·          PRT y
·          PRT 4
 
·          Exit

Post a Reply

Replies

Oldest  Newest  Rating
-1 Ed Gon · November 28, 2014
the library "string" results to an error but when i change it with "string.h" it went well though.
0 Ed Gon · November 28, 2014
thanks for the help! But I found 3 errors with it, "strlen" and "stoi" is not declared with this scope :S
0 Franz Schmidt · November 27, 2014
So I made a example how you could do it, but it's not complete you have to do the input exceptions
and also be careful if the user enters a decimal number.

If you need some special information about strings look it up here:
http://www.oopweb.com/CPP/Documents/CPPHOWTO/Volume/C++Programming-HOWTO-7.html

#include <iostream>
#include <string>
using namespace std;

int main(){
string cmd;
long long ax = 0;
bool error = false;

cout << "Type in command (type \"HELP\" to see all commands): " << endl;
while(1){
error = false;
cout << ">";
getline(cin, cmd);
if (cmd.find("ADD") == 0){
if (cmd.find(" ") == strlen("ADD")){
string x = cmd.substr(strlen("ADD "), cmd.length()-(cmd.length() - cmd.find(",") + strlen("ADD ")));
string y = cmd.substr(cmd.find(",") + 1);
ax = stoi(x) + stoi(y);
}
else{
error = true;
}
}
else if (cmd.find("PRT AX") == 0 && cmd.length() == strlen("PRT AX")){
cout << ">AX = " << ax << endl;
}
else if (cmd.find("HELP") == 0 && cmd.length() == strlen("HELP")){
cout << ">" << endl;
cout << ">ADD x,y add x and y and save the result in AX" << endl;
cout << ">PRT AX display the value of AX" << endl;
cout << ">EXIT exit the application" << endl;
cout << ">" << endl;
}
else if (cmd.find("EXIT") == 0 && cmd.length() == strlen("EXIT")){
break;
}
else{
error = true;
}

if (error){
cout << ">Error: Unknown command!" << endl;
}
}
}
  • 1

C++

107,122 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator