// namevalue.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "../../std_lib_facilities.h"
vector<string>names;
vector<double>scores;
vector<double>found_scores;
int find_name = 0, name_num = 0, find_score = 0;
bool found_name = false;
string specif_name = "", quit = "";
string name = "";
double score, specif_score = 0;
int report_names()
{
cout << "Enter a name or score: ";
cin.clear();
if(cin >> specif_score);
else {cin.clear();
cin >> specif_name;
}
if(specif_name!= ""){
for(find_name; find_name < names.size(); ++find_name)
if(specif_name == names[find_name]){
found_name = true;
name_num = find_name;
find_name = names.size();
}
}
else {
for(find_score; find_score < scores.size(); ++find_score)
if(specif_score == scores[find_score]){
found_name = true;
found_scores.push_back(find_score);
find_name = names.size();
}
}
if(specif_name != "") {
if(found_name == true) cout << names[name_num] << "==" << scores[name_num] << endl;
else cout << "Name not found.\n";
}
else if(specif_name == ""){
for(int i =0; i < found_scores.size(); ++i) {
int p = found_scores[i];
if(found_name == true) cout << names[p] << "==" << scores[p] << endl;
else if(found_name != true) {
cout.clear();
cout << "Score not found. \n";
}
}
}
else;
found_scores.erase(found_scores.begin(), found_scores.end());
find_score = 0;
find_name = 0;
name_num = 0;
found_name = false;
specif_name = "";
cout << "\nQuit? (Yes or No) ";
cin >> quit;
if(quit == "No" || quit == "no"){
cout << endl;
report_names();
}
return 0;
}
int main()
try
{
cout << "Please enter a name and a score. Stop entering with 'NoMore 0': \n";
while(cin >> name >> score && name != "NoMore") {
for(int t = 0; t < names.size(); ++t)
if(name == names[t])error("Duplicate: ",name);
names.push_back(name);
scores.push_back(score);
}
report_names();
system("pause");
return 0;
}
catch (runtime_error e) {
cout << e.what() << '\n';
system("pause");
}
catch (...) {
cout << "Exiting" << '\n';
system("pause");
}
4 replies to this topic
#1
Posted 09 June 2011 - 08:58 AM
So I'm new to learning C++, and I'm trying to make a small program that allows you to enter names and scores, and then you can select which names/scores to output. The problem is that if you enter a score and it is not found, it does not output "Score not found". Line 49 shows the If statement containing the cout.
|
|
|
#2
Posted 09 June 2011 - 11:26 AM
Aviod comparing the values. For integers try,
And for strings -- try the function strcmp(),
Hope this will help!
if((specif_score / scores[find_score]) == 1) OR if((specif_score % scores[find_score]) == 0)
And for strings -- try the function strcmp(),
if(strcmp(specif_name,names[find_name]) == 0)
Hope this will help!
I think i'm able to write a code for printing "Hello, World!". Proud of that!
#3
Posted 09 June 2011 - 11:43 AM
Quote
And for strings -- try the function strcmp(),
When I try that it says it can't convert the string to a const char. Is it because
names[find_name]
is a vector?
#4
Posted 09 June 2011 - 12:10 PM
As i can see above in your code, i think names is not an array it's just a vector<string>names; below in code you have specified it as an array.
if(specif_name == names[find_name])
I think i'm able to write a code for printing "Hello, World!". Proud of that!
#5
Posted 10 June 2011 - 09:29 AM
I don't think that's specifying it as an array, it's just calling all elements of the vector names through names[find_name] ++find_name
Sorry if I'm wrong
Sorry if I'm wrong
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









