This is how you read a file using getline:
while ( getline ( infile, buffer ) ) { // Process the line }
I don't understand that can u post it in my code?
This is how you read a file using getline:
while ( getline ( infile, buffer ) ) { // Process the line }
I don't understand that can u post it in my code?
int main()
{
string name;
cout << "Please enter the filename of the encoded message:";
getline(cin,name);
ifstream infile;
infile.open(name.c_str());
if (infile.fail()) {
cout << name << " fail to open." << endl;
exit(1);
}
cout << name << endl;
cout << " File successfully opened" << endl;
string buffer;
getline(infile, buffer);
while ( !infile.eof() ) {
cout << buffer << endl;
}
return 0;
}
Hi I'm making a program where the user types in the name of a file and displays the context of the file in the console window. I'm trying to use getline function to read from the stream. The problem is that it reads the file and displays ONLY the first line ( it displays the first line, but it' an infinite loop) of the file to the console screen. How do I get it to display all the lines of the file to the terminal? I know I could use some type of loop, but I don't know what to do for the loop. So how do I display the other lines?
http://www.cplusplus.com/doc/tutorial/arrays.html
read that it might help. I am kind of confused on what you need to do. What is readSeq supposed to return? for example what is the int that it returns supposed to represent? The most recently read number?
I understand a bit, but how do i store the sequence in the array given as a parameter?
//*****************************************************************************
// CPSC1620 - Fall 2008
//
//
// Date:
// Purpose: Program to perform array operations using functions
//*****************************************************************************
#include <iostream>
using namespace std;
//Prototype
int readSeq(int []);
int main()
{
return 0;
}
//*****************************************************************************
//Function: readSeq
//Assumption: Reads a sequence of integers
//Action: Stores the sequence in the array given as parameter
//*****************************************************************************
int readSeq(int [])
{
}
I'm not good with arrays I don't understand them. I have to make a function that reads squence of integers from the input and stores the sequence of numbers in the array give as a parameter. What would my first step be?
#include <iostream>
using namespace std;
int main ()
{
int q;
int j;
int sum = 0;
int p;
int num;
cout << " Please enter an even integer greater than 2: ";
cin >> num;
for ( int i = 2; i < num; i++ )
{
if ( ( i == 2 ) or ( i % 2 != 0 ) )
p = i;
for ( int j = 1; j <= p; j++ )
{
if ( p % j == 0 )
{
sum += j;
}
}
{
if ( sum == p + 1 )
q = p;
}
cout << q << endl;
}
return 0;
}
i'm trying to get the ODD numbers but its gives even numbers
You need brackets around what you want in the for-loop. No brackets means that only the next instruction is part of the loop. Put brackets around what needs to be repeated:
for (/* for-loop code */) { // code to be repeated. Inside brackets. } // code that is not repeated. Outside the brackets.
I've tried chaning the brackets and it still doesn't work.
The spacing in your code makes it very hard to read:
#include <iostream> using namespace std; int main () { int j; int t; int p; int num; cout << " Please enter an even integer greater than 2: "; cin >> num; for ( int i = 2 ; i < num; i++ ) if ( ( i == 2 ) or ( i % 2 != 0 ) ) { p = i; } { for ( int j = 2; j > p; j++ ) if ( p % j != 0 ) t = p; cout << t << endl; } return 0; }
You do realize that the top for-loop starts on line 14 and goes to line 18? Lines 20 - 26 are not part of the for-loop that starts on line 14.
ok how do I fix that?
#include <iostream>
using namespace std;
int main ()
{
int j;
int t;
int p;
int num;
cout << " Please enter an even integer greater than 2: ";
cin >> num;
for ( int i = 2 ; i < num; i++ )
if ( ( i == 2 ) or ( i % 2 != 0 ) )
{
p = i;
}
{
for ( int j = 2; j > p; j++ )
if ( p % j != 0 )
t = p;
cout << t << endl;
}
return 0;
}
I'm using this program to give me the prime numbers that are less than the number entered, but it's not working?
Ok so the code makes the user type in a number. Ok if the number is odd it must say "Invalid Input". If the number is EVEN it takes the ODD numbers of it and gives you the pairs that add up to the EVEN number.
For example if I type 8
I want
8 = 1 + 7
8 = 3 + 5
how do I extract the digits?
#include <iostream>
using namespace std;
int main()
{
int q;
int sum = 0;
int num;
cout << " Please enter a positive even interger ";
cin >> num;
if ( num % 2 != 0 )
cout << "Invalid Input" << endl;
else if ( num % 2 == 0 )
{ for ( int i = 1; i <= num; i++ ) {
if ( i % 2 != 0 )
( i = q , u );
}
if ( q + u == num )
cout << q << " + " << u << num << endl;
}
return 0;
}