C++ :: Outputting Contents Of Stack On Input Line?
Sep 3, 2013outputting the contents of my stack on the input line.
View 7 Repliesoutputting the contents of my stack on the input line.
View 7 Replieshow to stop outputting data based on a user input. The text file is as follows:
1. a
2. b
3. c
and the code I'm using is as follows:
int main (){
string line;
int search;
cout << "Enter a number from 1-3" << endl;
cin >> search;
search++;
ifstream myfile ("example.txt");
[Code]...
What I want to do is to just output number 1 (the whole line) if the user enters number 1. However, I get an error on the second condition w/c is the "&& line!= search"
i would like to read the content of a text file data.txt (line by line ) directly from the command line using this command: a.exe < data.txt.
What could be the c++ code to read/get the content of these lines (without using ifstream). The treatment of the lines is not a problem for me but i really don't know how to access the content of the file from the c++ code
I have written the following code but i am stuck. Write a program that will prompt the user for a file name and open that file for reading. Print out all the information in the file, numbering each new line of text.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
int main()
{
char line[81], filename[21], c;
int i = 1;
FILE *inFile;
[code]....
I'm coding a hangman game. I'm trying to store user entries so i can output them to show the user what they have already entered. Problem is that it's not display anything at all.
I'm trying to store multiple characters of course, and then display all characters stored.
char guess[27]={0};
cin >> guess[26];
int hit=0;
for(int i=0; i<len; i++) {
if( guess[26] == hidden_word[i] ) {
hit++;
select_word[i] = guess[26];
if(strcmp(hidden_word,select_word) == 0) {
[code]....
EDIT: I also get the error - Stack around the variable 'guess' was corrupted. At the end of the game.
i want to display all the contents of a file excluding one based on user input say i have 4 records in the file numbered 1 to 4....the user chooses 2 to exclude it outputs records 1,3,4,4 when it should be records 1,3,4 what am i doing wrong here is the code.its basically displaying the last record in the file twice
void excludeRecord()
{
ifstream read;
read.open("Data.txt");
int recordC =0;
string fnameC = " ";
string lnameC = " ";
[Code]...
HTML Code:
So I insert values from a vector into a list and into a multiset, and I noticed zero is added to their contents! I had to do a whole lot of debugging to find out where the error was, how can i stop this thing? Code which generates such error...
infact i checked the content of vector ups to be sure that there was no zero in it, but after loading into list combi_t * head, it seems like there was a zero added and this is giving me errors when i call function master_roller...
Code:
void ins(combi_t * &testa, int &numero, int &num, int &no)
{ // if (ricerca(testa, numero) == 0)
//{
combi_t *temp = new combi_t;
temp->val0 = numero;
temp->val1 = num;
temp->val2 = no;
temp->nextPtr = 0;
[Code] ....
I want to be able to read 2 line input, then safe them in separate arrays.
line1 input=array1
line2=array2
I am trying to get my input from user in the same line with certain width between but my output is not in the same line. my third variable which is fruit[i] is shown one line below . How I can fix this ?
#include <iostream>
using namespace std;
int main () {
int staff[3] , fruit[3];
[Code] ....
How to input two variables in the same line? Like when i type 12 and press enter, 1 must be assigned to a, and 2 must be assigned to b. I want it to be entered on the same line, and press enter only once. How do i do that?
View 5 Replies View RelatedI want the phrase "BUY TICKET" to trigger something, i'm thinking an if statement, and then assign the number after the phrase to another variable. There will also be other commands entered from the input file such as "BUY RAFFLE' "TOTAL REVENUE" on the following lines. Sounds like I need to compare a string(?) to a constant phrase, and if they match have it run the specific code?
Code:
//scan in from file
fscanf(ifp, "%c", &input);
if (input == 'BUY TICKET'){
//Run this
}
How to do it using the tools in C.
Here's my code, it's for a poker hand evaluator
input:
'ad 2d 3d 4d 5d...
...4s 5s 6s 7s 8s'
I think it should go through all the 10 crads and disply the name of each card, but it seems to only do it for the second hand
Code:
include <stdio.h>
#include <ctype.h>
int main( void ) {
char inputtedhand[64];
int z=0, counter=0;
int index;
int i=0,m=0,n=0;
[Code].....
We need a program that takes three names ( first middle last ) and print it each one at a different line...
View 5 Replies View RelatedI am trying to use the command line to put a txt file in this code. I do Project->set project arguments in code blocks and nothing happens. How does one set up the arguments and where do i do it in codeblocks?
Code:
int main(int argc, char *argv[]) {
char name[20];
struct artist *ptr;
char lineBuffer[LINEBUFFERSIZE];
artistNodePtr startPtr = NULL; /* initially the artist list is empty */
FILE *musicFile;
char *artistTemp, *discTemp, *yearTemp, *trackTemp, *songTemp;
[Code] ....
I have three questions concerning the below code I have written, which is expected to do basically as the attached diagram illustrates:
Code:
#include <stdio.h>
#include <stdlib.h>
double getAverageGrade(int studentGrades[] , int size);
int* getStudentInfo(int * numCourses);
int main()
[Code] ...
(1) How may I display the averages to 2 decimal places, namely why doesn't %.2lf work? The program gives 89.00 for 89 and 90, for instance.
(2) How may I read the number of courses and the relevant grades in a row for each student?
(3) How may I free (de-allocate) my array of pointers, namely grades? Does it suffice to use free(grades)?
I'm new to C/C++. I am attemping to use fgets and sscanf to read a line of input, and confirm it is a positive number.My code works great, except for the case of a negative number. When I enter a negative number, my while loop seems to run infinitely, with stdin providng the same input over and over again.
Here's the code snippet:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TEXT_LEN 64
void foo() {
char* inStr = (char*)malloc(sizeof(char)*TEXT_LEN);
memset(inStr, 0, TEXT_LEN);
}
[code]....
I've been stuck on this for hours, and have scoured the internet for any insight on this particular issue with no luck.
I'll simplify the problem here. I have a lines in an input file:
John Smith 1234 Main St New York
Carol Thomas 5678 5th Ave Columbus
...
and I am reading into character arrays using code like this:
infile.getline(name,20);
infile.getline(street,15);
infile.getline(city,10);
but when I print the output using something like this:
outfile << "Owner Name: " << name << endl;
outfile << "Address: " << street << city << endl;
(let's pretend I included spaces between the address components, which I omitted to save space)
I get an output file of:
Owner Name: John Smith
Address:
The output stops after the name variable, which I believe is stored correctly. I don't think anything is storing in the address pieces, this is the problem.
I think it's because I'm not using getline() properly in this case. Can you string together multiple getline()s like that?
If this was my design, I'd read in strings instead, but that is not possible in this case.
I want to input the elements of a two dimensional array in the same line . So while giving input . when i press enter . it should remain on the same line ? HOW to do that ?
View 1 Replies View RelatedI am having problems printing "->" on the beginning of each line, im trying to do it as a loop and ending it when the user types "q". also i would like to know how to ignore text from the user when the input begings with a specific character. heres what ive done so far, and not currrently working as expected.
Code:
#include <stdio.h>
int main (void) {
char prompt;
printf("~~~ FRACTION CALCULATOR ~~~
[Code] ....
I want the numbers to be put into the array until it hits a new line. Maximum number of numbers is 1000. What can I replace that while loop with to stop it from reading further into the input file?
Code:
while (input != '
'){
for(i=0; i<1000; i++)
fscanf(ifp, "%lf", &auctions_prices[i]);
I want to be able to write a program where I can record user input but combine it with command line arguments. For example I do a simple getline(); and the user types test how to combine it so it could be like test /q and it gives a different output depending on what additional input was added. Can this be done with lots of different inputs?
View 8 Replies View RelatedI am having an issue with the statement "Both the input and output files' names should be read from the command line." I don't understand what this means or what I need to do.
View 4 Replies View RelatedI'm trying to create a program that takes an integer input and displays each digit on a new line from right to left.
An example of this would be:
Enter an integer: 657
Digit (1): 7
Digit (2): 5
Digit (3): 6
So far I have this:
#include <stdio.h>
#include <math.h>
int main() {
int i, input, output;
printf("Enter an integer: ");
scanf("%d", &input);
if(input == 0)
printf("Digit (1): 0");
[Code] ....
It works perfectly fine, but I just learned that I am not allowed to use the #include <math.h> so I have to take out floor, log, and pow.
I'm pretty sure I have to do some sort of while or for loop to get the results, but how to do it.
How to get this thing to work. All i need to do is ask user to input a name and then it brings out the line from the .txt file containing the information.
For example in my case I'm doing a member search function I'm required to ask user to input the name of the customer and then print out all the details (which consumes 1 text line in the .txt file)
Here is the code, This is the write to text file method (100% working)
Code:
cout << "Customer Name: ";
cin >> name;
// ...
ofstream myfile("customer.txt", ios::app);
[Code] .....
How can a mulitline string be read line by line
ex: str = "PERIOD="week"
DAY="day"
TIME="time"";
When I run the program, the prompt I am giving the user before I collect their data, shows up twice in row. Here's the part of the code I'm referring to:
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
// method prototypes
void characterInstructions();
void phraseInstructions(); //
[Code] ....