The word "getline" is a red and underlined, which indicate an error:
#include <iostream>
using namespace std;
string ReadName()
{
string Name;
cout << "Please Enter Your Name: " << endl;
getline(cin, Name);
return Name;
}
void PrintName(string Name)
{
cout << "\nYour name is " << Name << endl;
}
int main()
{
PrintName(ReadName());
return 0;
}
The VS Gives an error regarding getline
The problem is it prints the full name but not the rest of the lines about the person.
Could someone, please guide me?
I do really appreciate your help!
auto itr = find(my_vec.begin(), my_vec.end(), search );
if(itr != my_vec.end())
{
std::cout << "Match found " << search << std::endl;
std::cout << "\nFull name: " << search << std::endl;
} else {
std::cout << "Match not found "<< std::endl;
}
There are a few style problems with your code:
No need to explicitly initialize strings, they will be empty by default (see here).
Keep a consistent style. For example, either always start brackets in the same line as the function signature or in the next line.
No need to close the file explicitly at the end of the function, this is done when the object goes out of scope (see (destructor) here).
No need to include <map> and <iomanip> headers.
Don't keep unused variables.
Give suggestive names to your variables.
Do not return error codes to the OS when the app is working as it should. Not finding a name is not an error, is it?
It seems your file has 6 entries per contact, so all you have to do is print 5 more lines. You do not need to store the lines in a vector, just parse and print them as you go. Here is an example:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
void findContact(std::string fullName, std::string contactListPath) {
std::ifstream inFile{contactListPath};
if (!inFile) {
std::cerr << "File could not be open!" << std::endl;
return;
}
std::string line;
while (std::getline(inFile, line)) {
if (line == fullName) {
std::cout << "Match found: \n";
std::cout << "\nFull name: " << fullName;
std::cout << "\nAddress: " << (std::getline(inFile, line), line);
std::cout << "\nE-mail: " << (std::getline(inFile, line), line);
std::cout << "\nPhone: " << (std::getline(inFile, line), line);
std::cout << "\nBirthday: " << (std::getline(inFile, line), line);
std::cout << "\nNote: " << (std::getline(inFile, line), line) << std::endl;
return;
}
}
std::cout << "Match not found " << std::endl;
}
int main() {
std::string fullName;
std::string contactListPath;
std::cout << "Enter full name to search: ";
std::getline(std::cin, fullName);
std::cout << "Enter path to contact list: ";
std::getline(std::cin, contactListPath);
findContact(fullName, contactListPath);
return 0;
}
If every entry contains 6 lines. Then you can print all the lines starting from the line that you found like:
auto itr = find(my_vec.begin(), my_vec.end(), search );
if(itr != my_vec.end())
{
std::cout << "Match found " << std::endl;
// print the next 6 lines
for(int remaining = 6;remaining > 0 && itr!=my_vec.end(); itr++,remaining--) {
std::cout << *itr << std::endl;
}
} else {
std::cout << "Match not found "<< std::endl;
}
Hello Everyone! I have a Project due tomorrow and I accidently chosen a hard one and now im regretting it. Im trying to make digital voting system on a small scale just to represent my idea as I'm a 2nd semester student of BSCS. I made the entire code by myself. The first code I made was working code but the output was not as I desired. I wanted to change the Vote after casting the vote. But it wasnt changing it. This is the first code Below:
#include<iostream>
#include<conio.h>
using namespace std;
void VoteLIST();
void VoteReturn();
class NADRA
{
protected:
double nic[4] = { {42401335},{1},{2},{3} };
int knum[4] = { {123},{1},{2},{3} };
int vote[4] = { {0},{0},{0},{0} };
void voteCHANGE(int a, int b)
{
vote[a] = b;
}
};
class CNIC :protected NADRA
{
private:
//nic = <=
double cnic;
int KhandaanNUM;
public:
CNIC()
{
cnic = KhandaanNUM = 0;
}
CNIC(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
void SetDATA(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
double getCNIC()
{
return cnic;
}
int getKNUM()
{
return KhandaanNUM;
}
int voteSTATUS = 0;
bool check()
{
for (int loop = 0; loop < 4; loop++)
{
if (cnic == nic[loop] && KhandaanNUM == knum[loop] && (vote[loop] == 0))
{
voteSTATUS = vote[loop];
return true;
break;
}
}
return false;
}
void setVOTE(int c)
{
//vote[voteSTATUS] = c;
voteCHANGE(voteSTATUS, c);
}
void VoteReturn()
{
cout << voteSTATUS;
}
};
class VOTEmenu
{
private:
CNIC c1;
double Cnic;
int kNum;
public:
void GetData()
{
cout << "Enter CNIC: ";
cin >> Cnic;
cout << "Enter Khandaan Number: ";
cin >> kNum;
c1.SetDATA(Cnic, kNum);
}
void checkData()
{
if (c1.check() == true)
{
int choice;
system("cls");
VoteLIST();
cout << endl;
cout << "Please choose the Respective Party Number" << endl;
cin >> choice;
c1.setVOTE(choice);
system("cls");
cout << "You have successfully voted for Party Number: " << choice <<endl;
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
void Data2()
{
if (c1.check() == true)
{
system("cls");
cout << "Your CNIC Number: " << Cnic << endl;
cout << "Khandaan Number: " << kNum << endl;
cout << "You Have Voted for party number: "; c1.VoteReturn();
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
};
void main()
{
while (true)
{
VOTEmenu v;
int choice;
system("cls");
cout << "Welcome To Vote Menu!" << endl;
cout << "Please choose any one of the following" << endl;
cout << "1) Cast Vote\n2) Check Vote";
cout << endl;
cin >> choice;
v.GetData();
if (choice == 1)
{
v.checkData();
_getch();
}
else if (choice == 2)
{
v.Data2();
_getch();
}
else
{
cout << "Wrong DATA:" << endl;
}
}
}
void VoteLIST()
{
cout << "VOTE LIST" << endl;
cout << "1: PTI\n2:PMLN\n3:PPP\n4:MQM" << endl;
}
Now I tried to remake the code and added file stream. taking text from external source instead of making array inside the visual studio and assigning values. but now Im facing an error which says This declaration has no storage class or type specifier The code is below:
#include<iostream>
#include<conio.h>
#include <fstream>
using namespace std;
void VoteLIST();
void VoteReturn();
class NADRA
{
protected:
fstream Text;
int nic[2];
int knum[2];
int vote[2];
int count = 0;
int ccnic = 0;
int knumm = 0;
int votee = 0;
Text.open("Nadra.txt")
{
while (!Text.eof())
{
Nadra >> ccnic >> knumm >> votee;
nic[count] = ccnic;
knum[count] = knumm;
vote[count] = votee;
count++;
}
}
Text.close();
void voteCHANGE(int a, int b)
{
vote[a] = b;
}
};
class CNIC :protected NADRA
{
private:
//nic = <=
double cnic;
int KhandaanNUM;
public:
CNIC()
{
cnic = KhandaanNUM = 0;
}
CNIC(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
void SetDATA(double a, int b)
{
cnic = a;
KhandaanNUM = b;
}
double getCNIC()
{
return cnic;
}
int getKNUM()
{
return KhandaanNUM;
}
int voteSTATUS = 0;
bool check()
{
for (int loop = 0; loop < 4; loop++)
{
if (cnic == nic[loop] && KhandaanNUM == knum[loop] && (vote[loop] == 0))
{
voteSTATUS = vote[loop];
return true;
break;
}
}
return false;
}
void setVOTE(int c)
{
//vote[voteSTATUS] = c;
voteCHANGE(voteSTATUS, c);
}
void VoteReturn()
{
cout << voteSTATUS;
}
};
class VOTEmenu
{
private:
CNIC c1;
double Cnic;
int kNum;
public:
void GetData()
{
cout << "Enter CNIC: ";
cin >> Cnic;
cout << "Enter Khandaan Number: ";
cin >> kNum;
c1.SetDATA(Cnic, kNum);
}
void checkData()
{
if (c1.check() == true)
{
int choice;
system("cls");
VoteLIST();
cout << endl;
cout << "Please choose the Respective Party Number" << endl;
cin >> choice;
c1.setVOTE(choice);
system("cls");
cout << "You have successfully voted for Party Number: " << choice << endl;
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
void Data2()
{
if (c1.check() == true)
{
system("cls");
cout << "Your CNIC Number: " << Cnic << endl;
cout << "Khandaan Number: " << kNum << endl;
cout << "You Have Voted for party number: "; c1.VoteReturn();
}
else
{
system("cls");
cout << "You are not Eligible to Cast Vote!" << endl;
cout << "Reasons:\n1) CNIC or Khandaan Number You entered may be Incorrect";
cout << "\n2) You may not be 18 Above\n3) You may have already casted the vote";
}
}
};
void main()
{
while (true)
{
VOTEmenu v;
int choice;
system("cls");
cout << "Welcome To Vote Menu!" << endl;
cout << "Please choose any one of the following" << endl;
cout << "1) Cast Vote\n2) Check Vote";
cout << endl;
cin >> choice;
v.GetData();
if (choice == 1)
{
v.checkData();
_getch();
}
else if (choice == 2)
{
v.Data2();
_getch();
}
else
{
cout << "Wrong DATA:" << endl;
}
}
}
void VoteLIST()
{
cout << "VOTE LIST" << endl;
cout << "1: PTI\n2:PMLN\n3:PPP\n4:MQM" << endl;
}
Can Someone please help??? Tomorrow is my last date! I just want my program to take data from text, chech the data with user entered data, IF correct, it will proceed and show for vote menu. if not, It will give error etc.
I know I'm a very pathetic learner. But You guys are professional! I will learn alot from you. Please help me!
For a school c++ lab (using microsoft visual studio, hence the system("pause")) I am making a program that will let a user input an email address and the program will spit out the username (before '#') and the site type, either the type based on the last three letters of the address (com is commercial ventures) or the last two letter country code (us is united states).
#include <iostream>
#include <string>
using namespace std;
void getemail(string &email);
void finduser(string email);
void findsitetype(string email);
int main()
{
string email;
getemail(email);
finduser(email);
findsitetype(email);
system("pause");
return 0;
}
void getemail(string &email)
{
cout << "Please enter your email address: ";
cin >> email;
cout << endl;
}
void finduser(string email)
{
int index = email.find('#');
cout << "Username: ";
for (int i = 0; i < index; i++)
cout << email[i];
cout << endl << endl;
}
void findsitetype(string email)
{
int truesize = size(email);
string lastthree;
for (int i = 0; i < 3; i++)
{
lastthree[i] = email[truesize - i];
}
cout << "Site type: ";
if (lastthree == "edu")
cout << "Educational institutions";
if (lastthree == "org")
cout << "Not-for-profit organizations";
if (lastthree == "gov")
cout << "Government entities";
if (lastthree == "mil")
cout << "Military installations";
if (lastthree == "net")
cout << "Network service providers";
if (lastthree == "com")
cout << "Commercial ventures";
if (email[truesize - 2] == '.')
cout << "Country Code " << email[truesize - 1] << email[truesize];
}
When I run the code, it spits out the username but there seems to be a fatal error when finding the site type. I think it has something to do with my incorrect string use? Any help appreciated.
Debug Assertion Failed!
Program: C:\windows\SYSTEM32\MSVCP140D.dll File: c:\program files
(x86)\microsoft visual studio 14.0\vc\include\xstring Line: 1681
Expression: string subscript out of range
For more information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
There are a couple things wrong with your code, (1) to get the length of a string, use the length() function, so:
int truesize = size(email);
should be
int truesize = email.length();
I changed your if statements to else ifs because if one of the condition statements evaluates to true, we shouldn't need to check the rest of them.
(2) your for loop is grabbing the email extension in the reverse direction, change:
for (int i = 0; i < 3; i++)
{
lastthree[i] = email[truesize - i];
}
to
lastthree = email.substr(truesize-3, truesize);
Hello and thanks in advance.
my problem today is i'm getting back "garbage" for the strings for a registry key value name, and value data. this appears to be a problem on all registry values within a key/folder except for the last value, though sometimes I am able to read the name of the first value (but still not the data)
what I'm trying to do is be able to display the value name and value data that are in a single, possibly variable, registry key (I dont care about subkeys at this point)
I'm trying to do this with windows unicode strings, WCHAR * and LPWSTR types.
the "garbage" i see for the problem strings is a repeated series of non-english characters, which messes up the subsequent wcout displays.
in the registry editor display, the values i'm attempting to read have REG_SZ type data, which I understand to be a string.
probably my biggest problem is i cannot find a clear guide on simply how to do exactly what I'm trying to do, look inside a registry key, and list the value names and value data. any help would be greatly appreciated. I am new to unicode strings and the windows api. my environment is windows xp sp3, visual c++ 2010 express.
#include <stdio.h>
#include <iostream> /* for std::wcin and wcout */
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <process.h>
#include "conio.h"
#
include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
int return_val;
DWORD error;
HKEY hkey;
BYTE iterations = 0;
/* 1. first, open key (folder) */
return_val = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"HARDWARE\\DEVICEMAP\\VIDEO", 0, KEY_READ, &hkey);
/* check how the open went */
if(return_val != ERROR_SUCCESS)
{
std::wcout << L"error opening the key: " << return_val << std::endl;
}
else
{
std::wcout << L"it's cool" << std::endl;
/* 1.a. query the key (optional) */
LPTSTR className = new WCHAR[255];
DWORD classNameSize = MAX_PATH;
DWORD subKey = 0;
DWORD maxSubKey;
DWORD maxClass;
DWORD value;
DWORD maxValue;
DWORD maxValueData;
DWORD securityDescriptor;
FILETIME ftLastWriteTime;
DWORD sectionNameSize;
return_val = RegQueryInfoKey(hkey, className, &classNameSize, NULL,
&subKey, &maxSubKey, &maxClass, &value, &maxValue,
&maxValueData, &securityDescriptor, &ftLastWriteTime);
std::wcout << L"query return: " << return_val << std::endl;
std::wcout << L"class name: " << className << L", (size): " << classNameSize << std::endl;
std::wcout << L"subkey: " << subKey << L", max: " << maxSubKey << L", maxClass: " << maxClass << std::endl;
std::wcout << L"Value: " << value << L", maxval: " << maxValue << L", maxvaldata: " << maxValueData << std::endl;
std::wcout << L"Sec descrpt: " << securityDescriptor << std::endl << std::endl;
/* now enumerate the strings in the key */
int count = 0;
DWORD valuename_size = 16, type_return = 0, data_size = 102;
LPWSTR valuename = new WCHAR[valuename_size];//maxValue
LPWSTR data = new WCHAR[data_size];//maxValueData>>1
/* 2. the outer loop grabs the values one at a time (the data within the key/folder) */
do {
iterations++; /* just for debugging */
return_val = RegEnumValue(hkey, count, valuename, &valuename_size, 0, &type_return, (LPBYTE)data, &data_size);
/* return of RegEnumValue */
std::wcout << L"RegEnumValue return val: " << return_val << std::endl;
/* double check sizes */
std::wcout << L"size: valname_size: " << valuename_size << L", data_size: " << data_size << std::endl;
if(return_val == ERROR_SUCCESS || return_val == ERROR_MORE_DATA)
{
/* to try and avoid bad strings */
if(type_return == REG_DWORD || count == 0)
std::wcout << L"Value - " << valuename << L", Type: " << type_return << L" Data - " << (BYTE)(*data) << std::endl;
else
std::wcout << L"Value - " << valuename << L", Type: " << type_return << L" Data - " << data << std::endl;
}
//data = REG_SZ;
count++;
} while (return_val != ERROR_NO_MORE_ITEMS && count < value);
}
/* just to check my code */
std::wcout << L"iterations: " << iterations << std::endl;
/* to "pause" during debugging */
std::wcin >> input;
return 0;
}
this version based in part on the commentors help seems to work how I want, and I'm posting this for others to reference. What wasnt clear to me is that when getting the number of characters back for the valuename did not include a null terminating character (of course) but also that the size of buffer you pass into it needs to include that, so if you get back 16 and you input 16, thats why it would return 234, (not fair that the input follows diferent constraints than the output but life's not fair), you then need to input 17 for the string size
/* now enumerate the strings in the key */
int count = 0;
DWORD valuename_size, type_return = 0, data_size;
LPWSTR valuename;
BYTE *data;
/* 2. the outer loop grabs the values one at a time (the data within the key/folder) */
do {
valuename_size = maxValue;
data_size = maxValueData;
iterations++; /* just for debugging */
return_val = RegEnumValue(hkey, count, NULL, &valuename_size, 0, &type_return, NULL, &data_size); /* value name */
//return_val = RegEnumValue(hkey, count, NULL, NULL, 0, &type_return, NULL, &data_size); /* value data */
valuename = new WCHAR[valuename_size+1];
data = new BYTE[data_size]; /* data_size is in BYTES, of any type */
valuename[0] = L'\0'; /* if the string returned is still invalid, this will help make sure wcout doesnt mess up */
return_val = RegEnumValue(hkey, count, valuename, &valuename_size, 0, &type_return, (LPBYTE)data, &data_size); /* value name */
//return_val = RegEnumValue(hkey, count, NULL, NULL, 0, &type_return, (LPBYTE)data, &data_size); /* value data */
/* return of RegEnumValue */
std::wcout << L"RegEnumValue return val: " << return_val << std::endl;
/* double check sizes */
std::wcout << L"size: valname_size: " << valuename_size << L", data_size: " << data_size << L", Type: " << type_return << std::endl;
if(return_val == ERROR_MORE_DATA /*&& type_return == REG_DWORD*/)
{
/* try again? */
delete valuename;//free(valuename);
delete data;
/* double the "global" max number of WORDs for the string (including null termination) */
maxValue <<= 1;
valuename_size = maxValue;
maxValueData <<= 1;
data_size = maxValueData;
/* doublecheck */
std::wcout << L"new val size before enum: " << valuename_size << L", new data size before enum: " << data_size << std::endl;
return_val = RegEnumValue(hkey, count, NULL, &valuename_size, 0, &type_return, NULL, &data_size); /* value name */
/* the return version of valuename_size is the number of characters, not including the null terminating character */
valuename = new WCHAR[valuename_size+1];
data = new BYTE[data_size];
valuename[0] = L'\0'; /* if the string returned is still invalid, this will help make sure wcout doesnt mess up */
return_val = RegEnumValue(hkey, count, valuename, &valuename_size, 0, &type_return, (LPBYTE)data, &data_size); /* value name */
std::wcout << std::endl << L"return: " << return_val << L", Val size: " << valuename_size << L", data_size: " << data_size << std::endl << std::endl;
}
if(return_val == ERROR_SUCCESS)
{
valuename[valuename_size] = L'\0'; /* null terminate the string before printing */
/* I only care about string data */
if (type_return == REG_SZ)
{
data[data_size] = 0; /* null terminate the string before printing */
std::wcout << L"Value - " << valuename << L", Type: " << type_return << L" Data - " << (LPWSTR)data << std::endl;
}
}
count++;
} while (return_val != ERROR_NO_MORE_ITEMS && count < value);
}
/* just to check my code */
std::wcout << L"iterations: " << iterations << std::endl;
/* to "pause" during debugging */
std::wcin >> input;
return 0;
}
A few obvious problems:
When you call RegEnumValue, valuename_size and data_size should contain the size of the buffers, but they only do so for the first value. For the second value and beyond they contain the results of the previous call.
String values in the registry are not guaranteed to be NULL terminated. You need to explicitly terminate strings (using the returned length) before using them. Note that since you're using unicode you need to divide the byte length by two to get the length in code units. These comments only apply to values, not names.
You assume that any value that isn't a DWORD is a string. What about binary values?