I am attempting use ifstream to extract two numbers from a file in argv[1], named "inputFile", and the extracting operator seems to be extracting the bits of code rather than the numbers needed.
inputFile.txt was put into the command line operator by right clicking the project, going to properties -> debugging -> command arguments -> typing inputFile.txt into command arguments in visual studio 2017.
The file inputFile.txt is as below:
1 2
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
//Test opening file
cout << "Input file: " << argv[1] << endl;
ifstream in(argv[1]);
if (!in)
{
cerr << "Unable to open " << argv[1] << " for input";
return 1;
}
//extract numbers
int num1;
int num2;
in >> num1 >> num2;
cout << num1 << endl << num2 << endl;
in.close();
return 0;
}
I expect the int num1 to hold 1, and the int num2 to hold 2, but instead each variable holds the number -858993460.
Are you sure the file you are reading has got the data you expect? This code works fine for me, compiling with Visual Studio 2005. But, If I change the contents of the file, writing not numbers (for example if I write: a b), num1 and num2 ends with -858993460.
I've try this case but I got stuck in a trouble. I just want to to determine length of a string from user input or from console. But it doesn't work.
#include <iostream>
#include <string>
using namespace std;
int main(){
int N;
string S;
char vocal;
vocal = {'a'||'i'||'u'||'e'||'o'};
cout << "Length of Spaghetti Name: ";
cin >> N;
fflush(stdin);
cout << "Spaghetti Name: ";
cin >> S;
if (S[0] != vocal){
S.erase(0,1);
} else if (S[N] != vocal){
S.erase(N,1);
}
cout << S << endl;
cin.get();
return 0;
}
expected input
9
Carbonara
expected output
arbonara
This program will delete non vocal character in spaghetti names.
Could anyone help me to upgrade my code ?
I bet the function could be done in shorter and more elegant way, could you show me the proper direction or examples how to write that?
Thanks for the replies, here's the code ( written in Codeblocks 16.01):
#include <iostream>
#include <string>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int D,a,b,bb,liczba,dlugosc;
int main()
{
cout << "liczba testow: ";
cin >> D;
while(D<11 && D>0){
for (int j=0;j<D;j++){
cin >> a >> b;
if(a<0 | a>1000000000 | b<0 | b>1000000000){
break;
}
liczba =a;
for(int i=1;i<b;i++){
liczba = liczba*a;
}
char aa[10] = {0};
itoa(liczba,aa,10);
string bb = aa;
int dlugosc = bb.length();
cout << bb << endl;
cout << bb[dlugosc-1] << endl;
a = 0;
}
return 0;
}
return 0;
}
I have the following console app in Visual Studio
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
wcout << "displayStringsTest_data" << endl;
wcout << L"列举" << endl;
wcout << "Done" << endl;
return 0;
}
But the Chinese characters are output as question marks
Does anyone know how I can get them output in the correct form
Thanks
Even though you are sending correct Chinese symbols to console.
Windows setting should be changed so that it supports those language specific characters.
You need to change PC language to Chinese from "Region and Language" section from control panel, which is by default to US English.
I have a very strange problem. Here is my code:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\n');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
This code doesn't end when I push Enter at all; therefore there is no way to complete the input. The output will stop like this and wait:
what is your name?
However if I change '\n' to 'p' or whatever char, it will finish the input when the specific char is input. For instance:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\p');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
Screen:
what is your name?
hello, Frank, how are you today?
RUN SUCCESSFUL (total time: 2s)
PS:
I am using NetBeans IDE 8.0 and Windows 8 Pro x64
New Line character depends on the operating system.
Reminder - \r\n or \n\r?
I would leave out the third parameter of the getline call all together. This will use getline's default behavior and terminate the read on the enter key.
EDIT: After reading you are still having problems, I decided to run some tests on your code.
First, to confirm that the default behavior works without specifying the third parameter to the getline method.
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
while(true){
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name);
cout << "hello, " << user_name << ", how are you today?" << endl;
}
}
The output:
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
Second, to test manually specifying the escape sequence '\n' i did this:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
while(true){
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\n');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
}
And the output:
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteBoardDev
hello, WhiteBoardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
compiled on g++ w/osx so i didnt bother testing the '\r\n' case. This brings up a good point about cross platform compatibility. If you want your code to work in multiple environments try to not specify anything platform specific in your code (aka the EOL sequence) and leave out that third parameter entirely.
Now I have solved my question with the help of WhiteboardDev. In my coding environment, I should use '\r' and during the input I need push Enter and Space, two keys in order to complete the it.