Hey guys I think I'm really close with but I'm not too sure how to continue. All of the questions related to my problem don't really answer anything. The error that I'm getting right now is an
(33): error C2064: term does not evaluate to a function taking 1 arguments
(41): error C2064: term does not evaluate to a function taking 1 arguments
Header file:
using namespace std;
class romanType
{
public:
void printRoman(char romanNum);
int printDecimal(int& total);
int convertRoman(int& total);
void setRoman(char& roman);
romanType();
romanType(char);
private:
char romanNum[6];
int decimal;
int total;
};
Implementation:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "romanType.h"
using namespace std;
romanType::romanType(char)
{
};
void romanType::printRoman(char romanNum)
{
cout << "Here is your number in Roman Numeral form: " << romanNum << endl;
};
int romanType::printDecimal(int& total)
{
cout << "Here is your number in Decimal form: " << total << endl;
return total;
};
void romanType::setRoman(char& romanNum)
{
};
int romanType::convertRoman(int& total)
{
int len = 0;
len = strlen(romanNum);
int count[1];
for(int i = 0; i < len; i++)
{
switch(romanNum[i])
{
case 'M':
count[i] = 1000;
break;
case 'm':
count[i] = 1000;
break;
case 'D':
count[i] = 500;
break;
case 'd':
count[i] = 500;
break;
case 'C':
count[i] = 100;
break;
case 'c':
count[i] = 100;
break;
case 'L':
count[i] = 50;
break;
case 'l':
count[i] = 50;
break;
case 'X':
count[i] = 10;
break;
case 'x':
count[i] = 10;
break;
case 'V':
count[i] = 5;
break;
case 'v':
count[i] = 5;
break;
case 'I':
count[i] = 1;
break;
case 'i':
count[i] = 1;
break;
default:
cout << "Error.." << endl;
}
total = total + count[0];
}
return total;
};
My main:
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include "romanType.h"
using namespace std;
int main()
{
romanType r;
char romanNum;
char choice;
int decimal;
int total;
cout << "Hello! Please enter your Roman Numeral: " << endl;
cin >> romanNum;
cout << endl;
r.setRoman(romanNum);
r.convertRoman(total);
cout << "Do you want the Roman Numeral or the Decimal?" << endl;
cout << "Press [D] for Decimal!" << endl << "Press [R] for Roman Numeral!" << endl;
cin >> choice;
if (choice == 'D' || choice == 'd')
r.printDecimal(total);
else if (choice == 'R' || choice == 'r')
r.printRoman(romanNum);
else
cout << "That wasn't the right button!" << endl;
system ("pause");
return 0;
}
I'm pretty sure I'm on the right track. It would be nice to see any tips or advice relating to my errors.
Thanks in advance
From just a quick look at the code, I might suggest looking through the debug window at the values of your variables at each step. What I am seeing is two variables, one char type named romanNum, and an entirely different one that is a char array named romanNum. It gets a little confusing but could work if you are only asking the user for a single char in roman numerals which then you wouldn't need an array at all. Otherwise, you could get a string then convert that into an array.
Start there and see if that helps.
Related
I'm making a game that tests the ASCII strength of a user versus a bot. (There is also a 2 player mode but that's working fine.) The full description is given at the top of my .cpp file. As a basic breakdown, the bot opens a txt file with 500 common four letter words and inserts them into a size 500 array. It then randomly generates a number to pick a random one, and then goes through the process of tovalue() to recieve its ASCII value, where in tovalue() runs through chartoint() four times, one for each character of the word. My issue is that the program calculates the ASCII value perfectly fine of the user generated word, but always returns 0 (0000) for the botword, no matter what the word.
I've tried a few iterations of the generateword() function including using a vector but always get the same resutls. I've done a lot of digging about this and haven't quite found any solutions, although I suspect that the chartoint() function could be better optimized, just not sure how to impliment any better solutions for this specific case. Also, don't think the problem is with chartoint() since it works fine for user input, but I'm pretty sure the problem is with generateword(). Suggestions for making chartoint() would be helpful, but its not my main priority right now since I just need the program to 100% work first. Also, I've confirmed that all of the words in my .txt file are all caps and only four characters per line.
// Write the code for a game called “ASCII Strength” of a four-letter word selected by Player 1
// followed by a four-letter word selected by Player 2. The result would be the sum
//of the ASCII value of each of the letters of the selected words and whoever has higher sum (called ASCII strength) wins.
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;;
int chartoint(char a) {
switch (a) {
case 'A':
return 1;
break;
case 'B':
return 2;
break;
case 'C':
return 3;
break;
case 'D':
return 4;
break;
case 'E':
return 5;
break;
case 'F':
return 6;
break;
case 'G':
return 7;
break;
case 'H':
return 8;
break;
case 'I':
return 9;
break;
case 'J':
return 10;
break;
case 'K':
return 11;
break;
case 'L':
return 12;
break;
case 'M':
return 13;
break;
case 'N':
return 14;
break;
case 'O':
return 15;
break;
case 'P':
return 16;
break;
case 'Q':
return 17;
break;
case 'R':
return 18;
break;
case 'S':
return 19;
break;
case 'T':
return 20;
break;
case 'U':
return 21;
break;
case 'V':
return 22;
break;
case 'W':
return 23;
break;
case 'X':
return 24;
break;
case 'Y':
return 25;
break;
case 'Z':
return 26;
break;
}
return 0;
}
int tovalue(string input) {
int first = chartoint(input[0]);
int second = chartoint(input[1]);
int third = chartoint(input[2]);
int fourth = chartoint(input[3]);
cout << first << second << third << fourth; // EXISTS TO TEST CALCULATION
int value = first + second + third + fourth;
return value;
}
string generateword() {
string arr[500];
ifstream file("words.txt");
if (file.is_open())
{
for (int i = 0; i < 500; i++) {
string temp;
getline(file, temp);
arr[i] = temp;
}
file.close();
}
else
{
cout << "Error: Unable to open file.";
exit(0);
}
srand(time(0));
int random_index = rand() % 500;
string random_word = arr[random_index];
return random_word;
}
int main()
{
cout << "Welcome to ASCII strength, a game where the strongest word wins!";
cout << "\nTo play, you must enter a four letter word. The program will calculate the 'ASCII strength' of your word and compare it to your opponent.";
cout << "\nWhoever has the higher sum will win!";
char another;
another = 'y';
while (another == 'y' || another == 'Y') {
cout << "\nWould you like to play against a friend, or against a bot? (F/B)";
char mode;
cin >> mode;
if (mode == 'F' || mode == 'f') {
cout << "\nPlayer 1, please input your four letter word in all caps: ";
string answer1;
cin >> answer1;
int value1;
value1 = tovalue(answer1);
cout << "\nPlayer 2, please input your four letter word in all caps: ";
string answer2;
cin >> answer2;
int value2;
value2 = tovalue(answer2);
if (value1 > value2) {
cout << "\nPlayer 1 wins!";
}
else if (value2 > value1) {
cout << "\nPlayer 2 wins!";
}
else if (value1 == value2) {
cout << "\nTie!";
}
}
else if (mode == 'B' || mode == 'b') {
cout << "\nPlease input your four letter word in all caps: ";
string answer;
cin >> answer;
int valueanswer;
valueanswer = tovalue(answer);
string botword;
botword = generateword();
cout << "\nThe bot generates a random word based on a list of popular four letter words.";
cout << "\nThe bot has generated this word: " << botword;
int valuebot;
valuebot = tovalue("botword");
cout << valueanswer << " " << valuebot; // THIS EXISTS PURELY TO TEST WHETHER THE VALUES ARE PROPERLY CALCULATING
if (valueanswer > valuebot) {
cout << "\nYou win!";
}
else if (valuebot > valueanswer) {
cout << "\nThe bot wins!";
}
else if (valueanswer == valuebot) {
cout << "\nTie!";
}
}
cout << "\nWould you like to start a new game? (y/n)";
cin >> another;
}
}
Your problem is this line:
valuebot = tovalue("botword");
Since all characters in "botword" are lowercase, you get all 0 score. You probably meant to write
valuebot = tovalue(botword);
I am writing a guessing game program using functions for each thing, I keep getting errors saying function isn't set so when I try to call it, it isn't working. I can't figure out what I am doing wrong.
I know I have arguments for the functions that aren't being used but I cant seem to figure out where or how I should include those in the function themself.
I am fairly new to programming/c++ so please no negative comments I am just trying to get as much help as I can.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int getGuess(string prompt);
string getRank(int guessCount);
bool getPlayAgain(string prompt);
void playOneGame();
int main(){
srand(time(0));
int number = rand() % 100 + 1;
string prompt = getGuess();
do(
playOneGame();
)while(getPlayAgain())
return EXIT_SUCCESS;
}
int getGuess(string prompt){
int num;
int guessCount = 0;
prompt = cout << "Please enter a number between 1-100: ";
cin >> num;
if(num > 100){
cout << "Please enter a number between 1-100: " << endl;
}
if(num < 1){
cout << "Please enter a number between 1-100: " << endl;
}
if(num <= 100){
cout << "The number you guessed is: " << num << endl;
guessCount++;
}
}
string getRank(int guessCount){
switch(guessCount){
case 1:
case 2:
case 3: cout << "Lucky!" << endl;
break;
case 4:
case 5:
case 6: cout << "Awesome";
break;
case 7:
case 8:
case 9: cout << "Good";
break;
case 10:
case 11:
case 12: cout << "Meh";
break;
case 13:
case 14:
case 15: cout <<"Poor";
break;
default: cout << "Pathetic";
}
}
bool getPlayAgain(string prompt){
bool done = false;
int num1;
while(!done){
cout << "Enter 1 to play again or 2 to quit: ";
cin >> num1;
if(num1 == 2){
break;
}
else(
getGuess();
)
}
}
void playOneGame(){
getGuess();
getRank();
getPlayAgain();
}
No return statement in getguess() function but function signature is int return type.
Getguess() accepts prompt parameter as input but not used inside the function.
I'm kinda new to C++ and I'm having an hard time with a little ""game"" I'm making.
Here I'm tryng to create a sort of menu that you can move through using the Arrow keys as inputs, and confirm using the Enter key, but despite not getting any error in the compiler the program just closes itself when I reach the second Switch Case.
What am I doing wrong? Thanks
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <string>
#include <windows.h>
#include <conio.h>
#define KEY_UP 72
#define KEY_DOWN 80
#define KEY_LEFT 75
#define KEY_RIGHT 77
#define ENTER 13
using namespace std;
int main()
{
struct Stats {
int Hp;
int Atk;
int Speed;
int Defense;
};
struct Defense {
bool IsWeakFire;
bool IsWeakIce;
bool IsWeakWind;
bool IsWeakElectric;
bool IsWeakLight;
bool IsWeakDark;
};
struct CharacterStats {
string Name;
string Desc;
string Persona;
Stats Stats;
Defense Defense;
}Characters[4];
//Program Start
cout << "You are now playng: ProjectPB \n" << endl <<
"MC Name> ";
cin >> Characters[0].Name;
cout << "\n \n The game will now start.";
for (int i=0;i<4;i++)
{
Sleep(500);
cout << ".";
}
system("cls");
//Fight start
int i = 0;
int sel = 1;
int c = 0;
while (i == 0) {
switch (sel)
{
case 1:
system("cls");
cout << " |ATTACK| PERSONA DEFEND" << endl;
system("pause");
break;
case 2:
system("cls");
cout << " ATTACK |PERSONA| DEFEND" << endl;
system("pause");
break;
case 3:
system("cls");
cout << " ATTACK PERSONA |DEFEND|" << endl;
system("pause");
} break;
Sleep(100);
int i = 0;
int sel = 1;
while (i == 0){
switch (sel)
{
case 1:
system("cls");
cout << " |ATTACK| PERSONA DEFEND" << endl;
system("pause");
break;
case 2:
system("cls");
cout << " ATTACK |PERSONA| DEFEND" << endl;
system("pause");
break;
case 3:
system("cls");
cout << " ATTACK PERSONA |DEFEND|" << endl;
system("pause");
} break;
Sleep(100);
int c = _getch();
switch (c)
{
case KEY_LEFT :
sel--;
if (sel <= 0)
{
sel = 3;
}
break;
case KEY_RIGHT :
sel++;
if (sel > 3)
{
sel = 1;
}
break;
case ENTER:
i = 1;
break;
}
}
}
return 0;
}
This must be because of the non-empty input buffer during this statement
int c = _getch();
To solve this,simply clear the buffer with this right before getch()
while ((getchar()) != '\n');
I'm having trouble converting using toupper on the first character in my string.
I used tolower(first[0]) to turn the first letter into lower case.
Why doesn't toupper(first[0]) make the first character upper case?
Also, is there a way to move the first character in a string to the last spot?
Thanks a lot in advance.
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char ans;
do{
string first, last;
char first_letter, first_letter2;
cout << "This program will convert your name "
<< "into pig latin.\n";
cout << "Enter your first name: \n";
cin >> first;
cout << "Enter your last name: \n";
cin >> last;
cout << "Your full name in pig latin is ";
for(int x = 0; x < first.length(); x++){
first[x] = tolower(first[x]);
}
for(int x = 0; x < last.length(); x++){
last[x] = tolower(last[x]);
}
first_letter = first[0];
bool identify;
switch (first_letter)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
identify = true;
break;
default:
identify = false;
}
if(identify == true){
toupper(first[0]);
cout << first << "way" << " ";
}
first_letter2 = last[0];
bool identify2;
switch (first_letter2)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
identify2 = true;
break;
default:
identify2 = false;
}
if(identify2 == true){
toupper(first[0]);
cout << last << "way" << endl;
}
cout << "You you like to try again? (Y/N)\n";
cin >> ans;
} while(ans == 'y' || ans == 'Y');
return 0;
}
Just a simple blunder, compare
first[x] = tolower(first[x]);
with
toupper(first[0]);
usual case of the 'can't see the obvious thing missing' syndrome... I hate those mistakes.
As for moving the first character to the end I'd usually just use substr() for a simple case:
str = str.substr(1) + str[0];
I'm having trouble with the hexadecimal part of my c++ program. When I use the switch for hexadecimal nothing returns. also for some reason my binary conversion has a leading 0 I cant seem to get rid of.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <array>
using namespace std;
void binary(int, int);
void hex(int, int);
int _tmain(int argc, _TCHAR* argv[])
{
int numb, base;
cout << "Enter a decimel number : ";
cin >> numb;
cout << "Enter a base you want number switched to: ";
cin >> base;
switch (base){
case 2: binary(numb, base); break;
case 8: break;
case 16: hex(numb, base); break;
default: break;
}
}
void binary(int numb, int base)
{
int bin[32] = { 0 };
int i = 0;
do
{
bin[i] = numb%base;
numb = numb / base;
i++;
} while (numb != 0);
cout << "\n";
while (i >= 0){
cout << bin[i];
i--;
}
cout << endl << endl;
}
void hex(int numb, int base){
int i;
int hex[10] = { 0 };
for (i=0; i > 10; i++){
hex[i] = numb%base;
numb = numb / base;
for (i; i > 0; i--)
{
if (hex[i] >= 10)
{
switch (hex[i]){
case 10: cout << "A"; break;
case 11: cout << "B"; break;
case 12: cout << "C"; break;
case 13: cout << "D"; break;
case 14: cout << "E"; break;
case 15: cout << "F"; break;
default: break;
}
}
cout << hex[i];
}
}
cout << endl;
}
binary
The problem is after the first loop, i is one greater than the last index. Just for example, say you enter 1: the do...while loop is entered, the digit 1 is put in array index 0, then i is incremented to 1.
Then, in the second loop, both indexes 1 and 0 are printed. You can solve this by decrementing i before entering this loop:
i--;
while (i >= 0){...}
You should be doing something like that anyway, because if you ended up using all 32 digits, you would try to access bin[32] and the program may crash or output gibberish.
hex
The first loop's condition is infinite:
for (i = 0; i >= 0; i++){...}
It should be the same as your condition in binary:
for (i = 0; numb != 0; i++){...}
But you are not done yet because I've noticed you also have a bug in your printing:
if (hex[i] >= 10)
{
switch (hex[i])
{
case 10:
cout << "A";
break;
...
}
}
cout << hex[i];
If hex[i] is greater than or equal to 10, it gets printed twice, once as a hex letter and once as a decimal number. To solve this you could, for example, use continue instead of break in your switch (to skip the second print), or use else:
if (hex[i] >= 10)
{
switch (hex[i])
{
case 10:
cout << "A";
break;
...
}
}
else
{
cout << hex[i];
}
You also need to make the same correction as in binary:
// decrementing i before entering the loop
// vvv
for (i--; i >= 0; i--){...}
Your revision is not correct, hex should not have a nested loop. It was fine before, just with the corrections I've pointed out.