Not getting output

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 532 Views - Last Post: 24 February 2012 - 09:41 PM Rate Topic: -----

#1 java101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 31-October 11

Not getting output

Posted 23 February 2012 - 06:22 PM

I am working on i/o files, the code I have generated The input file and it says "this is a testt This is also a test, this is yet another test. in the ouput put file, Im trying to get the all the T's capitalized. This is what I have so far. I know I want to create a string, Im lost at where to begin. I am trying to learn more about how i/o works. Thanks

  
#include "stdafx.h" 
#include <iostream> 
#include <iomanip> 
#include <string> 
#include <fstream> 
using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) { 
	
	ifstream in("A06input.txt"); // Open for reading 
	ofstream out("A06output.txt"); // Open for writing string s; 
	
	int strLen = 0; 
	
	
	
	return 0; 

}




Is This A Good Question/Topic? 0
  • +

Replies To: Not getting output

#2 tlhIn`toq  Icon User is offline

  • Closing in on 5,000
  • member icon

Reputation: 4926
  • View blog
  • Posts: 10,461
  • Joined: 02-June 10

Re: Not getting output

Posted 23 February 2012 - 06:45 PM

C++ Read text file, line by line
Was This Post Helpful? 1
  • +
  • -

#3 java101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 31-October 11

Re: Not getting output

Posted 23 February 2012 - 06:52 PM

Thank You.
Was This Post Helpful? 0
  • +
  • -

#4 java101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 31-October 11

Re: Not getting output

Posted 23 February 2012 - 07:39 PM

Ok so I have read some articles and I need to creat a string where I need to read everyline and make the t's capital. Im stuck, this is the string I have created so far, thanks

string s = "t";  

  

 s[0] = toupper(s[0]);   

 cout << s << endl;

Was This Post Helpful? 0
  • +
  • -

#5 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 33
  • View blog
  • Posts: 146
  • Joined: 19-September 11

Re: Not getting output

Posted 23 February 2012 - 07:44 PM

You could also code this the old fashioned way with a simple for loop.
Here's an example of what I mean:

for(i = 0; i < strlen(i); i++){
    if(string[i] == 't'){ string[i] -= 32; }
}



This just uses the ascii values to alter from upper to lower case.
Hope this helps.

EDIT: here is a full on example that I just ran to make sure it works

#include <stdio.h>
#include <string.h>

int main(){
	char input[3];
	input[0] = 't';
	input[1] = 't';
	input[2] = 'T';
	int i = 0;
	
	for(i = 0; i < strlen(input)-1; i++){
    		if(input[i] == 't'){ input[i] -= 32; }
	}
	
	printf("%c%c%c\n",input[0],input[1],input[2]);
}


This post has been edited by raspinudo: 23 February 2012 - 07:50 PM

Was This Post Helpful? 2
  • +
  • -

#6 java101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 31-October 11

Re: Not getting output

Posted 23 February 2012 - 08:05 PM

Thank you, When I am working with output files I am not sure why I am not able to compile. thanks so much. I am looking at articles and tutorials, I m just not able to put it all together, I guess. Im trying to learn C++
Was This Post Helpful? 0
  • +
  • -

#7 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 33
  • View blog
  • Posts: 146
  • Joined: 19-September 11

Re: Not getting output

Posted 23 February 2012 - 08:15 PM

View Postjava101, on 23 February 2012 - 08:05 PM, said:

Thank you, When I am working with output files I am not sure why I am not able to compile. thanks so much. I am looking at articles and tutorials, I m just not able to put it all together, I guess. Im trying to learn C++


No problem man. For learning the basics there are many great options to get you started, here are a few links that really helped me along the way.

Great Video Tutorial Series: http://thenewboston....list.php?cat=16
^ These really got me started in C++

Great reference material: http://www.cplusplus.com/
^This site is great for looking up different functions, data structures and all of their respective nuances.

Good written tutorials you may find useful: http://www.cprogramming.com/
^This site also has helpful practice problems

Also, be sure to check out all of the beginner tutorials here on dic, many of them have helped me tremendously.

Good luck and happy coding.
Was This Post Helpful? 2
  • +
  • -

#8 java101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 31-October 11

Re: Not getting output

Posted 23 February 2012 - 08:25 PM

thank you. I figured that I need to use the getline() function within a while loop to process the lines of the file.
getline(in, s) not for sure how to incorporate this in my code. thanks Im working on it now
Was This Post Helpful? 0
  • +
  • -

#9 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 33
  • View blog
  • Posts: 146
  • Joined: 19-September 11

Re: Not getting output

Posted 23 February 2012 - 08:34 PM

For getline, you can use a loop like this:

while (getline(file, input) {
/*this stores the current line in the string variable input
here you can do whatever you want with it, then the loop will
grab the next line
*/
}


Was This Post Helpful? 0
  • +
  • -

#10 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 968
  • View blog
  • Posts: 3,379
  • Joined: 19-February 09

Re: Not getting output

Posted 23 February 2012 - 08:36 PM

With C++ strings there is the length() (or size()) member function.

string::operator[]
Was This Post Helpful? 1
  • +
  • -

#11 raspinudo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 33
  • View blog
  • Posts: 146
  • Joined: 19-September 11

Re: Not getting output

Posted 23 February 2012 - 08:39 PM

View Post#define, on 23 February 2012 - 08:36 PM, said:

With C++ strings there is the length() (or size()) member function.

string::operator[]


This is true, I am still in 'C mode' haha.
Although, just so you know java101, most all C code will be syntactically valid in C++.
Was This Post Helpful? 1
  • +
  • -

#12 java101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 31-October 11

Re: Not getting output

Posted 23 February 2012 - 08:39 PM

I am trying to make all the "t's" in the lines : This is a test, This is also a test, This is yet another test capitol. I cannot find a tutorial to show me how to write code to find all the "t's" and make them capitol. please point me in the right direction. Thanks
Was This Post Helpful? 0
  • +
  • -

#13 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 968
  • View blog
  • Posts: 3,379
  • Joined: 19-February 09

Re: Not getting output

Posted 23 February 2012 - 08:59 PM

Probably, the first thing to do is read each line of the input file and print it to the screen or to the file.

When that is done you can think about checking each character in the line to discover the lowercase t's.

Then you can consider how to alter them.

A string is a series of characters and you can access any character in the string and alter them if you wish.

Here you need to move along the string using a loop as in the link I posted earlier.

 string s = "t";  
  
 s[0] = toupper(s[0]);   

 cout << s << endl;

This post has been edited by #define: 23 February 2012 - 09:07 PM

Was This Post Helpful? 0
  • +
  • -

#14 java101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 31-October 11

Re: Not getting output

Posted 23 February 2012 - 09:14 PM

Thanks Everyone for the help/tutoral pointers. So this is the code I have come up with and I think I am back where I started from , no output :(


#include "stdafx.h" 
#include <iostream> 
#include <iomanip> 
#include <string> 
#include <fstream> 
using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) { 
	
	ifstream in("A06input.txt"); // Open for reading 
	ofstream out("A06output.txt"); // Open for writing string s; 
	
	 string s; 
	 int strLen = 0;
		
	
     int i = 0;  
     for(i = 0; i < strlen("This is a test"); i++){  
		 {
           cout << s[i];
		 }
		   
		 { 
		 
		 while(getline(in,s));
	 
	 }  

 
system("pause");
return 0;

} 


So I tried to rewrite the code, as from the beggining, and now I have one failed error. Please assist;

#include "stdafx.h" 
#include <iostream> 
#include <iomanip> 
#include <string> 
#include <fstream> 
using namespace std; 

int _tmain(int argc, _TCHAR* argv[]) { 
	
	ifstream in("A06input.txt"); // Open for reading 
	ofstream out("A06output.txt"); // Open for writing string s; 
	
	 string s; 
	 int strLen = 0;
		
	
     int i = 0;  
     for(i = 0; i < strlen("This is a test"); i++){  
		 {
           cout << s[i];
		 }
		   
		 { 
		 
		 while(getline(in,s));
	 
	 }  

 
system("pause");
return 0;

} 

Was This Post Helpful? 0
  • +
  • -

#15 jimblumberg  Icon User is online

  • member icon

Reputation: 3041
  • View blog
  • Posts: 9,276
  • Joined: 25-December 09

Re: Not getting output

Posted 23 February 2012 - 09:49 PM

Post the complete error message exactly as it appears in your development environment. These messages have important information embedded in them to make the locating and fixing the errors easier.

Jim
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2