c++ void
there seem have a problem at line line 28,64,66
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
|
#include <iostream>
using namespace std;
void concatonate(){
string first,second,third,all;
cout << "Enter your First Name: ";
cin >> first;
cout << "Enter your Middle Name: ";
cin >> second;
cout << "Enter your Surname: ";
cin >> third;
all = first + " " + second + " " + third;
cout << all;
}
void lenght(){
char word;
cout << "Enter a word ";
while(!(cin >> word)){
cout << "Integers are Invalid: ";
cin .clear();
cin.ignore(100 , '\n');
}
cout << "Word: \n" << word;
cout << "contains:" << word.length();
}
void changeletter(){
char first[100],second;
cout << "Enter a string: ";
cin >> first;
cout << "Change the First letter: ";
cin >> second;
first[0] = second;
cout << first;
}
void array(){
char choice;
cout << "A.Single or B.Multi\n";
cin >> choice;
while(!(choice == 'a' || choice == 'A') && (choice == 'b' || choice == 'B')){
cout << "Must choose only the choices above: ";
cin >> choice;
cin.clear();
cin.ignore(100 , '\n');
}
if(choice == 'a' || choice == 'A'){
char chc;
cout << "A.Integer or B.String: ";
cin >> chc;
while(!(chc == 'a' || chc == 'A') && (chc == 'b' || chc == 'B')){
cout << "Must choose only the choices above: ";
cin >> chc;
cin.clear();
cin.ignore(100,'\n');
}
if(chc == 'a' || chc == 'A'){
singleint();
}else if(chc == 'b' || chc == 'B'){
singlestring();
}
}
}
void singleint(){
int num [3];
cout << "Enter three numbers: ";
for(int i = 0; i < 3; i++){
cin >> num[i];
}
cout << "The Numbers are: ";
for (int g = 0; g < 3; g++){
cout << num[g];
}
}
void singlestring(){
string name[5];
cout << "Enter 5 Names: ";
for (int i = 0; i < 5; i++){
cin >> name[i];
}
for(int g = 0; g < 5; g++){
cout << name[g];
}
}
int main (){
cout << "All CT-203\n";
char choice;
while(choice == 'y' || choice == 'Y'){
char opt;
cout << "A.Concatonate\n";
cout << "B.Lenghts of a string\n";
cout << "C.Changing a Letter \n";
cout << "D.Array\n";
cin >> opt;
while(!(opt == 'a' || opt == 'A') && (opt == 'B' || opt == 'B') && (opt == 'c' || opt == 'C') && (opt == 'd' || opt == 'D')){
cout << "Only enter in the options above: ";
cin.clear();
cin.ignore(100, '\n');
cin >> opt;
}
if(opt == 'A' || opt == 'a'){
concatonate();
cout << "Would you like to make another transaction? [Y/N]";
cin >> choice;
while(!(choice == 'y' || choice == 'Y') && (choice == 'n' || choice == 'N')){
cout << "Choose ONLY [Y/N]: ";
cin >> choice;
cin.clear();
cin.ignore(100 , '\n');
}
}else if (opt == 'B' || opt == 'b'){
lenght();
cout << "Would you like to make another transaction? [Y/N]";
cin >> choice;
while(!(choice == 'Y' || choice == 'y') && (choice == 'n' || choice == 'N')){
cout << "Choose ONLY [Y/N]: ";
cin >> choice;
cin.clear();
cin.ignore(100 , '\n');
}
}
}
}
|
Looks like you want word to be of type std::string.
You cannot call a function before it has been declared. Either move the definitions of singleint() and singlestring() above the definition of array()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void singleint(){
...
}
void singlestring(){
...
}
void array(){
...
singleint();
...
singlestring();
...
}
|
Or leave the definitions where they are and just declare (without defining) singleint() and singlestring() above array().
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
void singleint();
void singlestring();
void array(){
...
singleint();
...
singlestring();
...
}
void singleint(){
...
}
void singlestring(){
...
}
|
Last edited on
thank you, that worked. still having problems at 28 tho.
never mind i got 28
Without knowing the exercise requirements, consider something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
|
#include <iostream>
#include <string>
using namespace std;
void concatonate() {
string first, second, third;
cout << "Enter your First Name: ";
cin >> first;
cout << "Enter your Middle Name: ";
cin >> second;
cout << "Enter your Surname: ";
cin >> third;
cout << first + ' ' + second + ' ' + third;
}
void lenght() {
string word;
while (cout << "Enter a word " && !(cin >> word)) {
cout << "Invalid input\n";
cin.clear();
cin.ignore(100, '\n');
}
cout << "Word: " << word;
cout << "\ncontains: " << word.length() << " chars\n";
}
void changeletter() {
string first;
char second {};
cout << "Enter a string: ";
getline(cin >> ws, first);
cout << "Change the First letter to: ";
cin >> second;
first.front() = second;
cout << first;
}
void singleint() {
int num[3] {};
cout << "Enter three numbers: ";
for (int i = 0; i < 3; ++i)
cin >> num[i];
cout << "\nThe Numbers are: ";
for (int g = 0; g < 3; ++g)
cout << num[g] << ' ';
cout << '\n';
}
void singlestring() {
string name[5];
cout << "Enter 5 Names: ";
for (int i = 0; i < 5; ++i) {
cin >> name[i];
}
cout << "\nThe names are: ";
for (int g = 0; g < 5; g++) {
cout << name[g] << ' ';
}
cout << '\n';
}
void arrays() {
char choice { };
cout << "A. Single or B. Multi: ";
cin >> choice;
while (!(choice == 'a' || choice == 'A' || choice == 'b' || choice == 'B')) {
cout << "Must choose only the choices above: ";
cin.clear();
cin.ignore(100, '\n');
cin >> choice;
}
if (choice == 'a' || choice == 'A') {
char chc {};
cout << "A. Integer or B. String: ";
cin >> chc;
while (!(chc == 'a' || chc == 'A' || chc == 'b' || chc == 'B')) {
cout << "Must choose only the choices above: ";
cin.clear();
cin.ignore(100, '\n');
cin >> chc;
}
chc == 'a' || chc == 'A' ? singleint() : singlestring();
} else
cout << "[Multi not yet implemented]\n";
}
int main() {
for (char opt { 'A' }; !(opt == 'E' || opt == 'e'); ) {
cout << "\nA. Concatenate\n";
cout << "B. Lengths of a string\n";
cout << "C. Changing a Letter \n";
cout << "D. Array\n";
cout << "E. Exit\n";
cout << "Enter option: ";
cin >> opt;
switch (opt) {
case 'a':
case 'A':
concatonate();
break;
case 'b':
case 'B':
lenght();
break;
case 'c':
case 'C':
changeletter();
break;
case 'd':
case 'D':
arrays();
break;
case 'e':
case 'E':
break;
default:
cout << "Invalid option.\n";
cin.clear();
cin.ignore(100, '\n');
break;
}
}
}
|
Topic archived. No new replies allowed.