- 'strToDouble' was not declared in this scope Lab1-3.cpp /Lab1-3/src line 65 C/C++ Problem

- 29
- 3
-
You should show the actual compilation errors you're getting, and clearly indicate which line of the code is generating those compilation errors; instead of expecting everyone to figure out which line of code doesn't compile even before starting to figure out why. – Sam Varshavchik Jan 16 '18 at 21:37
-
This should be for code review web site. But 1) `using namespace std;` is bad - google that 2) `char title` -.> How does a title fit in one character? 3) Read up about `const` 5) Read up about buffer overrun 6) Read up about flushing `cout` – Ed Heal Jan 16 '18 at 21:37
-
... And read up on how to use a debugger – Ed Heal Jan 16 '18 at 21:38
-
1@EdHeal No, it shouldn't be on Code Review: they only accept the code that compiles, but this question is about compilation errors. – Algirdas Preidžius Jan 16 '18 at 21:39
-
@S01780 Please copy-paste the error messages that you are getting. Chances are - they tell **exactly** what's wrong with the code. – Algirdas Preidžius Jan 16 '18 at 21:42
-
@AlgirdasPreidžius - you are right - my mistake – Ed Heal Jan 16 '18 at 21:42
-
@AlgirdasPreidžius Yes I'm sorry about that, I've added the errors to the post. Thank you! – S01780 Jan 16 '18 at 21:47
-
Read the error message - think - https://ideone.com/xWyUec - I have fixed the compilation errors - do a comparison – Ed Heal Jan 16 '18 at 21:51
-
strToDouble() needs a prototype (forward declare) to remove that compiler error. getline() - wants a std::string, not a char. http://www.cplusplus.com/reference/string/string/getline/ – Michael Dorgan Jan 16 '18 at 21:52
-
1@S01780 1) "_C/C++ Problem_" There's no such language as "C/C++". 2) Did you try looking at the documentation of [std::getline](http://en.cppreference.com/w/cpp/string/basic_string/getline)? You would've noticed that there is no such overload, for how are you calling it. Did you mean to use `std::string` instead of `char`, for `title`? 3) `strToDouble` is undeclared at the point of usage, which is exactly, as stated in the error. 4) Consider learning from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) instead of coding randomly. – Algirdas Preidžius Jan 16 '18 at 21:53
-
@Algirdas Preidžius I'm sorry but what does overload mean? – S01780 Jan 16 '18 at 22:09
-
@S01780 Handy reading: [Override and overload in C++](https://stackoverflow.com/questions/429125/override-and-overload-in-c) – user4581301 Jan 16 '18 at 22:12
-
@S01780 You can learn that from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Algirdas Preidžius Jan 16 '18 at 22:13
-
@Algirdas Preidžius Thanks, I’ll check that out. – S01780 Jan 18 '18 at 00:09
2 Answers
The first problem, as @SoronelHaetir pointed out, is that you were trying to assign title
to variable which can only hold one character. Instead, you should use char array, char pointer, or even string object to contain your multi-letter value. In my code example below, I used char array with fixed size of 25, to store the title. Beware that you can store only up to 24 characters in it, because char arrays need special character which will denote the end of char array. Otherwise it would end up writing junk after your desired value. That special character is null-terminating character which is written like '\0'
.
Using return;
statement in your void displayBid(Info itemOne);
function was completely unnecesary. While you can use return;
to stop function from executing, you placed it at the end of function which was just about to end itself in normal way, but you forced it with no reason. Besides, you do not need any return;
statements for functions which return void – nothing.
Then, fund
and bidAmount
are representing money value, which may or may not be of integer value, so you should consider float
or double
data types to store money value.
Next thing is your function Info getBid();
. First, I have to say that naming may be a bit confusing. If you read the name of that function without seeing its actual code, how would you understand what it may do? For me, it sounded like it is about to get me information about a bid, while actually it is setting it up. Second, you could simplify code for entering values, in the way I did it in my code example. The way you tried to use different techniques for getting values from user input was a bit wrong. getline
is member function which is used with istream
objects. Your istream
object is cin
. In order to access that member function you shall write it as cin.getline(
to be discussed);
. That function only works with characters. Its first parameter accepts pointer to the first character (address of the first character) in sequence of characters.
Second parameter is of integer data type and specifies how much characters you want to be extracted from your input and stored in an argument which is in place of the first parameter. Beware not to write, for example, 25, because in char array you have to leave one place for '\0'
character, which is automatically placed where it needs to be. getline
member function has also default delimiter '\n'
, which denotes new line. It means that you can enter less characters than function can extract, because extraction will stop as soon as it reads that delimiter value from user input. Although, if you want your specific delimiter, getline
member function has its overloaded version which third parameter is one where you enter desired delimiter as an argument. (Overloaded functions are basically functions with the same name, but different parameters. They provide same functionality with different implementation.)
Even if you had set up values for a bid, you never returned it from function. You correctly said that its return value is Info
, but you did not return it. Actually, you again exited just before its normal exit. Instead, you should have written return itemOne;
In my code example, I passed the variable created in int main();
function by reference, which means it is not a copy as usually, so I do not have to return it and assign to another variable of the same type to appropriately apply desired changes.
Finally, in the int main();
function, you could just declare int choice
, without initializing it and use do-while loop in the way I did it. Also, switch statement provides defining what will happen if none of the cases are true, in the way that after all cases you write default:
, and below it whatever you want to happen. In your code example, your function will continue executing even if user enters anything but 1, 2 except for 9 defined to stop its execution. In my code example, whatever user enters besides 1 and 2, including zero, function will exit. Well, except for new line.
And, let us discuss again the naming. Your data structure name has to directly imply what it is. Info
does not do that. That name would actually be more appropriate for your void displayBid(Info itemOne);
function to be called. In my code example, I renamed it to Bid
.
#include <iostream>
using namespace std;
struct Bid
{
char title[25];
int vehicleID;
double fund;
double bidAmount;
};
void GetBid(Bid item)
{
cout << "Title: " << item.title << endl;
cout << "Fund: " << item.fund << endl;
cout << "Vehicle: " << item.vehicleID << endl;
cout << "Bid Amount: " << item.bidAmount << endl;
}
void SetBid(Bid & item)
{
cout << "Enter title: ";
cin >> item.title;
cout << "Enter fund: ";
cin >> item.fund;
cout << "Enter vehicle ID: ";
cin >> item.vehicleID;
cout << "Enter amount: ";
cin >> item.bidAmount;
}
int main()
{
Bid item;
int choice;
do {
cout << "Menu:" << endl;
cout << " 1. Enter Bid" << endl;
cout << " 2. Display Bid" << endl;
cout << " 0. Exit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice)
{
case 1:
SetBid(item);
break;
case 2:
GetBid(item);
break;
default:
choice = 0;
cout << "Goodbye." << endl;
break;
}
} while (choice != 0);
return 0;
}

- 43
- 1
- 6
-
This looks like great and thorough assistance. My one bit of advice is that it may be worth giving smaller bits of advice until a question author has proved they have made a great effort themselves. Unfortunately I have some helpers give very generously of their time only to find they don't get so much as a reply from the OP, who then never signs in again. Of course, answers are for future readers primarily, so this is not a disaster, but sometimes answers can be so localised they don't get the exposure that the educational opportunity deserves. – halfer Jan 17 '18 at 16:07
-
@halfer – Thank you very much for directing me in how to properly write answers. I will keep your advice in mind next time I will be writing answers. – sky3 Jan 17 '18 at 16:17
-
Well, it wasn't that it was not properly written - just that it is possible to "burn out" by spending an hour working on answers that sometimes go to waste. I upvoted! – halfer Jan 17 '18 at 16:18
-
1@halfer – Yes, I got your point. Next time I will try keep question author engaged in process of resolving their problem, in this kind of questions that demand many things to be corrected. Thank you! – sky3 Jan 17 '18 at 16:24
-
1@Purplette Thank you, that was very informative and helpful. I really appreciate your advice. Your example gives me something to work towards. :) – S01780 Jan 18 '18 at 00:08
The first (and biggest) problem is:
char title;
This allows you to store only a single character rather than an entire name (prefer std::string
to char
arrays).

- 19,824
- 17
- 99
- 186

- 14,104
- 1
- 12
- 23