In: Computer Science
I need assistance on what I am doing wrong, I've been trying to declare "getRandomLetter" as a scope, but haven't found how to all day. It's been about 3+ hours and I still have nothing. Please help fix these and let me know what I am doing wrong (There may be more simple ways of coding all this, but I just need help fixing the errors with current code, thank you). I am trying to have buildAcronym() hold the position of going from A-Z , & as well as getRandomLetter() choose a letter from A-Z using the ASCII character set. This is a bit of the prompt where it asks to set these up. If I am off, I do apologize.
Define and overload two void-typed functions named buildAcronym that assign a (pseudo)-random uppercase letter to two (2) or three (3) character parameters, depending on the version called. Each character in an acronym must be a unique uppercase letter. Two arguments (or three, depending on the version called) will be assigned a random character literal, which can then be printed out in main.
These are my errors.
Acronyms.cpp: In function 'void buildAcronym(char, char,
char)':
Acronyms.cpp:13:35: error: 'getRandomLetter' was not declared in
this scope
getRandomLetter(a , b , c)
^
Acronyms.cpp: In function 'void buildAcronym(char, char)':
Acronyms.cpp:25:24: error: 'getRandomLetter' was not declared in
this scope
getRandomLetter(a,b)
^
Acronyms.cpp: In function 'int main()':
Acronyms.cpp:50:39: error: 'getRandomLetter' was not declared in
this scope
cout << getRandomLetter(a,b) ;
^
Acronyms.cpp:56:42: error: 'getRandomletter' was not declared in
this scope
cout << getRandomletter(a,b,c);
^
Acronyms.cpp:60:9: error: expected ';' before '{' token
{
^
Acronyms.cpp:67:1: error: expected 'while' at end of input
}
^
Acronyms.cpp:67:1: error: expected '(' at end of input
Acronyms.cpp:67:1: error: expected primary-expression at end of
input
Acronyms.cpp:67:1: error: expected ')' at end of input
Acronyms.cpp:67:1: error: expected ';' at end of input
Acronyms.cpp:67:1: error: expected '}' at end of input
------------------------------------------------------------------------------------------------------------------------------------
--
--------------------------------------
(there are 3 lines above include Name: Date: Filename: apologies that it is missing)
#include
using namespace std;
void buildAcronym(char a ,char b , char c)
{
if('A'<= b && 'A' <= b && 'A' <= c
&& a <= 'Z' && b <= 'Z' && c <=
'Z')
{
getRandomLetter(a , b , c)
{
cout << a + rand() % 65 + 90 << "." ;
cout << b + rand() % 65 + 90 << "." ;
cout << c + rand() % 65 + 90 << "." ;
}
}
return ;
}
void buildAcronym(char a , char b)
{
getRandomLetter(a,b)
{
cout << a + rand() % 65 + 90 << "." ;
cout << b + rand() % 65 + 90 << "." ;
}
return ;
}
int main()
{
char a , b , c ;
int choice ;
do
{
cout << "Press 1 for a two letter acronym, and 2 for a three
letter acronym. " ;
cin >> choice ;
switch(choice)
{
case 1 :
if (choice == 1)
{
cout << getRandomLetter(a,b) ;
break ;
}
case 2 :
if (choice == 2)
{
cout << getRandomletter(a,b,c);
break;
}
else (choice != 1 && choice != 2)
{
cout << "Press 1 for a two letter acronym, and 2 for a three
letter acronym. ";
}
}while(choice!=1 && choice != 2);
return 0;
}
Here is your updated code, there were lots of redundant lines that was simplified. The entire program can be rewritten in a few lines but since you wanted to modify your code and make changes, I followed that only.
Here is the code with screenshot. Hope this is what you were looking for, If you get stuck let me know.
=========================================================================
#include <iostream>
#include<cstdlib> // for generating random number
#include<string>
using namespace std;
// returns a random letter from A to Z
char getRandomLetter(){
int range = 'Z'-'A';
return 'A' + rand()%range;
}
// overloaded takes 2 arguments
string buildAcronym(char firstLetter, char secondLetter)
{
return
string(1,firstLetter)+string(1,secondLetter);
}
// overloaded takes 3 arguments
string buildAcronym(char firstLetter, char secondLetter, char
thirdLetter)
{
return string(1,firstLetter)+string(1,secondLetter)
+string(1,thirdLetter);
}
char getUniqueLetter(char used_one, char used_two){
char a;
do{
a = getRandomLetter();
}while(a==used_one || a==used_two);
return a;
}
int main()
{
char a , b , c ;
int choice ;
cout << "1 for a two letter acronym\n2 for a three letter
acronym\n3 Quit\n\n";
do
{
cout<<"Enter choice: " ;
cin >> choice ;
switch(choice)
{
case 1 :
a = getUniqueLetter(b,c);
b=getUniqueLetter(a,c);
cout<<buildAcronym(a,b)<<endl;
break ;
case 2 :
a = getUniqueLetter(b,c);
b = getUniqueLetter(a,c);
c = getUniqueLetter(a,b);
cout<<buildAcronym(a,b,c)<<endl;
break;
case 3:break;
default:
cout<<"Press 1 or 2 or 3 only.\n Please try
again."<<endl;
}
}while(choice!=3);
return 0;
}
======================================================================